diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/README.md b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/README.md new file mode 100644 index 0000000..4903ab3 --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/README.md @@ -0,0 +1,4 @@ +# Motor Control Library for dsPIC33 DSCs +

The Motor Control Library is a collection of functions that you can use as building blocks for implementing Field Oriented Control (FOC) of 3-phase motor control applications on dsPIC® Digital Signal Controllers (DSCs).

+

+The Motor Control Library is architected to enable a user application to call time-critical control loop functions in it and take advantage of the motor control peripherals available on the dsPIC33 DSCs to implement a design.

\ No newline at end of file diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/libmotor_control_dspic-elf.a b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/libmotor_control_dspic-elf.a new file mode 100644 index 0000000..a30626a Binary files /dev/null and b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/libmotor_control_dspic-elf.a differ diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control.h b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control.h new file mode 100644 index 0000000..885dbd7 --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control.h @@ -0,0 +1,100 @@ +/******************************************************************************* + Motor Control Library Interface Header File + + File Name: + motor_control.h + + Summary: + This header file lists all the interfaces used by the Motor Control library. + + Description: + This header file lists the type defines for structures used by the Motor + Control library. Library function definitions are also listed along with + information regarding the arguments of each library function. This header file + also includes another header file that hosts inline definitions of certain + library functions. +*******************************************************************************/ + +// DOM-IGNORE-BEGIN +/* ********************************************************************* + * (c) 2017 Microchip Technology Inc. and its subsidiaries. You may use + * this software and any derivatives exclusively with Microchip products. + * + * This software and any accompanying information is for suggestion only. + * It does not modify Microchip's standard warranty for its products. + * You agree that you are solely responsible for testing the software and + * determining its suitability. Microchip has no obligation to modify, + * test, certify, or support the software. + + * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, + * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, + * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, + * AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH + * MICROCHIP PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY + * APPLICATION. + + * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, + * PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF + * ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF + * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE + * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL + * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT + * EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO + * MICROCHIP FOR THIS SOFTWARE. + + * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF + * THESE TERMS. + * + * *****************************************************************************/ +/******************************************************************************* +Note: +* Some parts of this header file are protected by #ifdef __XC16__. These protections + are provided to accommodate non-XC16 compilers to work with this header file. + Similarly, sections of the header file related to the MATLAB-based compiler are + protected by #ifdef __MATLAB_MEX__ protections. +* Some of the function declarations have a MC_ATTRB prefix. This prefix has been + provided as a placeholder for adding attributes for supporting future versions + of the compiler. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef _MOTOR_CONTROL_H_ // Guards against multiple inclusion +#define _MOTOR_CONTROL_H_ + +// ***************************************************************************** +// ***************************************************************************** +// Section: Included Files +// ***************************************************************************** +// ***************************************************************************** + +#include +#ifdef __XC16__ // See comments at the top of this header file +#include +#endif // __XC16__ + +#ifdef __cplusplus // Provide C++ Compatability + extern "C" { +#endif + +#ifdef __MATLAB_MEX__ // See comments at the top of this header file +#define inline +#endif // __MATLAB_MEX + + +#include "motor_control_declarations.h" +#include "motor_control_inline_declarations.h" + + +#ifdef __XC16__ // See comments at the top of this header file +#include "./motor_control_inline_dspic.h" +#endif // __XC16__ + +#ifdef __cplusplus // Provide C++ Compatibility + } +#endif +#endif // _MOTOR_CONTROL_H + + + + + diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_declarations.h b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_declarations.h new file mode 100644 index 0000000..572ddb5 --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_declarations.h @@ -0,0 +1,529 @@ +/******************************************************************************* + Motor Control Library Function Declaration Header File + + File Name: + motor_control_declarations.h + + Summary: + This header file lists all the function declarations used by the Motor Control library. + + Description: + This header file lists all the function declarations used by the Motor Control library. +*******************************************************************************/ + +// DOM-IGNORE-BEGIN +/* ********************************************************************* + * + * (c) 2017 Microchip Technology Inc. and its subsidiaries. You may use + * this software and any derivatives exclusively with Microchip products. + * + * This software and any accompanying information is for suggestion only. + * It does not modify Microchip's standard warranty for its products. + * You agree that you are solely responsible for testing the software and + * determining its suitability. Microchip has no obligation to modify, + * test, certify, or support the software. + + * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, + * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, + * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, + * AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH + * MICROCHIP PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY + * APPLICATION. + + * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, + * PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF + * ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF + * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE + * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL + * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT + * EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO + * MICROCHIP FOR THIS SOFTWARE. + + * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF + * THESE TERMS. + * + * *****************************************************************************/ +/******************************************************************************* +Note: +* Some of the function declarations have a MC_ATTRB prefix. This prefix has been + provided as a placeholder for adding attributes for supporting future versions + of the compiler. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef _MOTOR_CONTROL_DECLARATIONS_H_ // Guards against multiple inclusion +#define _MOTOR_CONTROL_DECLARATIONS_H_ + +// ***************************************************************************** +// ***************************************************************************** +// Section: Included Files +// ***************************************************************************** +// ***************************************************************************** + +#include +#include "motor_control_types.h" + +#if __XC16_VERSION__ > 1011 // Place-holder function attribute prefix +#define MC_ATTRB +#else +#define MC_ATTRB +#endif + +#ifdef __cplusplus // Provide C++ Compatability + extern "C" { +#endif + +// ***************************************************************************** +// ***************************************************************************** +// Section: Interface Routines +// ***************************************************************************** +// ***************************************************************************** + +/******************************************************************************* + Function: + uint16_t MC_CalculateSineCosine_Assembly_Ram(int16_t angle, MC_SINCOS_T *pSinCos) + + Summary: + This function calculates the Sine and Cosine values for a specified angle input. + + Description: + This function calculates the Sine and Cosine values for specified angle input using + linear interpolation on a sine table of 128 words. This routine works the same for + both integer input and 1.15 scaling input. + + Precondition: + None. + + Parameters: + angle - Input - This parameter is the input angle which will be used to calculate + the Sine and Cosine components. + pSinCos - Output - This parameter is a pointer to a MC_SINCOS_T type structure to + which the Sine and Cosine components of the angle are written. + + Returns: + Unsigned integer value '1' for direct look up and '2' for interpolation. + + Example: + + uint16_t temp; + int16_t angle; + MC_SINCOS_T mcSinCos; + temp = MC_CalculateSineCosine_Assembly_Ram(angle, &mcSinCos); + + + Remarks: + For integer scaling the Angle is scaled such that 0 <= Angle < 2*pi corresponds + to 0 <= Ang < 0xFFFF. The resulting Sine and Cosine values are returned scaled to + -32769 -> 32767 i.e. (0x8000 -> 0x7FFF). + For 1.15 scaling the Angle is scaled such that -pi <= Angle < pi corresponds to + -1 -> 0.9999 i.e. (0x8000 <= Ang < 0x7FFF). The resulting Sine and Cosine values are + returned scaled to -1 -> 0.9999 i.e. (0x8000 -> 0x7FFF). + *******************************************************************************/ +uint16_t MC_ATTRB MC_CalculateSineCosine_Assembly_Ram(int16_t angle, + MC_SINCOS_T *pSinCos); + +/******************************************************************************* + Function: + uint16_t MC_TransformParkInverse_Assembly( const MC_DQ_T *pDQ, const MC_SINCOS_T *pSinCos, + MC_ALPHABETA_T *pAlphaBeta) + + Summary: + This function calculates the inverse Park transformation. + + Description: + This function calculates the inverse Park transform on a pair of stationary reference frame inputs. + Inverse park transformation is performed as described by the equation: + + alpha = d*cos - q*sin + beta = d*sin + q*cos + + + Precondition: + None. + + Parameters: + pDQ - Input - This parameter is a pointer to a MC_DQ_T type structure. + pSinCos - Input - This parameter is a pointer to a MC_SINCOS_T type structure. + pAlphaBeta - Output - This parameter is a pointer to a MC_ALPHABETA_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_DQ_T mcVDQ; + MC_SINCOS_T mcSinCos; + MC_ALPHABETA_T mcVAlphaBeta; + temp = MC_TransformParkInverse_Assembly(&mcVDQ, &mcSinCos, &mcVAlphaBeta); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +uint16_t MC_ATTRB MC_TransformParkInverse_Assembly( const MC_DQ_T *pDQ, + const MC_SINCOS_T *pSinCos, + MC_ALPHABETA_T *pAlphaBeta); + +/******************************************************************************* + Function: + uint16_t MC_TransformClarkeInverseSwappedInput_Assembly( const MC_ALPHABETA_T *pAlphaBeta, MC_ABC_T *pABC) + + Summary: + This function calculates the scaled reference vectors using inputs in an alpha-beta reference frame. + + Description: + This function calculates the scaled reference vectors in an a-b-c reference frame using + inputs from an alpha-beta reference frame, as described by the equation: + + a = beta + b = -beta/2 + (sqrt(3)/2) * alpha + c = -beta/2 - (sqrt(3)/2) * alpha + + + This is a modified variant of the inverse Clarke transformation where alpha & beta are + swapped compared to the normal inverse Clarke transformation. This function is designed + to work with the CalculateSpaceVectorPhaseShifted() in order to simplify the calculation of + three-phase duty cycle values from a given set of inputs in the alpha-beta reference frame. + + Precondition: + None. + + Parameters: + pAlphaBeta - Input - This parameter is a pointer to a MC_ALPHABETA_T type structure. + pABC - Output - This parameter is a pointer to a MC_ABC_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ALPHABETA_T mcVAlphaBeta; + MC_ABC_T mcVabc; + temp = MC_TransformClarkeInverseSwappedInput_Assembly(&mcVAlphaBeta, &mcVabc); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +uint16_t MC_ATTRB MC_TransformClarkeInverseSwappedInput_Assembly( const MC_ALPHABETA_T *pAlphaBeta, + MC_ABC_T *pABC); + + +/******************************************************************************* + Function: + uint16_t MC_TransformClarkeInverse_Assembly( const MC_ALPHABETA_T *pAlphaBeta, MC_ABC_T *pABC) + + Summary: + This function calculates the scaled reference vectors using inputs in an alpha-beta reference frame. + + Description: + This function calculates the scaled reference vectors in an a-b-c reference frame using + inputs from an alpha-beta reference frame, as described by the equation: + + a = alpha + b = -alpha/2 + (sqrt(3)/2) * beta + c = -alpha/2 - (sqrt(3)/2) * beta + + + This is the conventional form of the inverse Clarke transformation. This function is designed + to work with the CalculateSpaceVector(). + + Precondition: + None. + + Parameters: + pAlphaBeta - Input - This parameter is a pointer to a MC_ALPHABETA_T type structure. + pABC - Output - This parameter is a pointer to a MC_ABC_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ALPHABETA_T mcVAlphaBeta; + MC_ABC_T mcVabc; + temp = MC_TransformClarkeInverse_Assembly(&mcVAlphaBeta, &mcVabc); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +uint16_t MC_ATTRB MC_TransformClarkeInverse_Assembly( const MC_ALPHABETA_T *pAlphaBeta, + MC_ABC_T *pABC); + +/******************************************************************************* + Function: + void MC_TransformClarkeInverseNoAccum_Assembly( const MC_ALPHABETA_T *pAlphaBeta, MC_ABC_T *pABC) + + Summary: + This function calculates the scaled reference vectors using inputs in an alpha-beta reference frame. + + Description: + This function calculates the scaled reference vectors in an a-b-c reference frame using + inputs from an alpha-beta reference frame, as described by the equation: + + a = alpha + b = -alpha/2 + (sqrt(3)/2) * beta + c = -alpha/2 - (sqrt(3)/2) * beta + + + This is the conventional form of the inverse Clarke transformation. This function is designed + to work with the function CalculateSpaceVector() for duty cycle generation. It does not make use + of the DSP accumulators. + + Precondition: + None. + + Parameters: + pAlphaBeta - Input - This parameter is a pointer to a MC_ALPHABETA_T type structure. + pABC - Output - This parameter is a pointer to a MC_ABC_T type structure. + + Returns: + void + + Example: + + MC_ALPHABETA_T mcVAlphaBeta; + MC_ABC_T mcVabc; + MC_TransformClarkeInverseNoAccum_Assembly(&mcVAlphaBeta, &mcVabc); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +void MC_ATTRB MC_TransformClarkeInverseNoAccum_Assembly( const MC_ALPHABETA_T *pAlphaBeta, + MC_ABC_T *pABC); + +/******************************************************************************* + Function: + uint16_t MC_CalculateSpaceVectorPhaseShifted_Assembly( const MC_ABC_T *pABC, uint16_t iPwmPeriod, MC_DUTYCYCLEOUT_T *pDutyCycleOut) + + Summary: + This function calculates the duty cycle values based on the three scaled + reference vectors in the a-b-c reference frame and the PWM period value. + + Description: + This function calculates the duty cycle values based on the three scaled reference + vectors in the a-b-c reference frame and the PWM period value. + + This function is designed to work with the TransformClarkeInverseSwappedInput() + in order to simplify the calculation of three-phase duty cycle values from a given + set of inputs in the alpha-beta reference frame. + This function uses a reference axis that is phase shifted by 30 degrees relative + to the standard Space Vector Modulation reference axis. This phase-shifted reference + axis is accommodated by using reference vector inputs from a modified version of + the inverse Clarke transform which swaps the alpha-beta values at its input. + + Precondition: + None. + + Parameters: + pABC - Input - This parameter is a pointer to a MC_ABC_T type structure. + iPwmPeriod - Input - This parameter is an unsigned integer value of the PWM period. + pDutyCycleOut - Output - This parameter is a pointer to a MC_DUTYCYCLEOUT_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ABC_T mcVabc; + uint16_t iPwmPeriod; + MC_DUTYCYCLEOUT_T mcDutyCycleOut; + temp = MC_CalculateSpaceVectorPhaseShifted_Assembly(&mcVabc, iPwmPeriod, &mcDutyCycleOut); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +uint16_t MC_ATTRB MC_CalculateSpaceVectorPhaseShifted_Assembly( const MC_ABC_T *pABC, + uint16_t iPwmPeriod, + MC_DUTYCYCLEOUT_T *pDutyCycleOut); + +/******************************************************************************* + Function: + uint16_t MC_CalculateSpaceVector_Assembly( const MC_ABC_T *pABC, uint16_t iPwmPeriod, + MC_DUTYCYCLEOUT_T *pDutyCycleOut) + + Summary: + This function calculates the duty cycle values based on the three scaled + reference vectors in the a-b-c reference frame and the PWM period value. + + Description: + This function calculates the duty cycle values based on the three scaled reference + vectors in the a-b-c reference frame and the PWM period value. + + This function works with the conventional Clark inverse transform variants + in order to simplify the calculation of three-phase duty cycle values from a given + set of inputs in the alpha-beta reference frame. + The duty-cycle generation part of the function is identical to the phase shifted version + for SVM generation, so the initial part of this function compensates for the + swapped input inverse clark function. This ensures the motor rotation direction remains + the same. + + Precondition: + None. + + Parameters: + pABC - Input - This parameter is a pointer to a MC_ABC_T type structure. + iPwmPeriod - Input - This parameter is an unsigned integer value of the PWM period. + pDutyCycleOut - Output - This parameter is a pointer to a MC_DUTYCYCLEOUT_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ABC_T mcVabc; + uint16_t iPwmPeriod; + MC_DUTYCYCLEOUT_T mcDutyCycleOut; + temp = MC_CalculateSpaceVector_Assembly(&mcVabc, iPwmPeriod, &mcDutyCycleOut); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +uint16_t MC_ATTRB MC_CalculateSpaceVector_Assembly( const MC_ABC_T *pABC, + uint16_t iPwmPeriod, + MC_DUTYCYCLEOUT_T *pDutyCycleOut); + +/******************************************************************************* + Function: + uint16_t MC_TransformClarke_Assembly( const MC_ABC_T *pABC, MC_ALPHABETA_T *pAlphaBeta) + + Summary: + This function calculates the Clarke transformation. + + Description: + This function transforms inputs in an a-b-c reference frame to an alpha-beta + reference frame using the equation: + + alpha = a + beta = a*(1/sqrt(3)) + 2*b*(1/sqrt(3)) + + + Precondition: + None. + + Parameters: + pABC - Input - This parameter is a pointer to a MC_ABC_T type structure. + pAlphaBeta - Output - This parameter is a pointer to a MC_ALPHABETA_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ABC_T mcIabc; + MC_ALPHABETA_T mcIAlphaBeta; + temp = MC_TransformClarke_Assembly(&mcIabc, &mcIAlphaBeta); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +uint16_t MC_ATTRB MC_TransformClarke_Assembly( const MC_ABC_T *pABC, + MC_ALPHABETA_T *pAlphaBeta); + + +/******************************************************************************* + Function: + uint16_t MC_TransformPark_Assembly( const MC_ALPHABETA_T *pAlphaBeta, const MC_SINCOS_T *pSinCos, MC_DQ_T *pDQ) + + Summary: + This function calculates the Park transformation. + + Description: + This function transforms inputs in an alpha-beta reference frame to a stationary + d-q reference frame using the equation: + + d = alpha*cos + beta*sin + q = -alpha*sin + beta*cos + + + Precondition: + None. + + Parameters: + pAlphaBeta - Input - This parameter is a pointer to a MC_ALPHABETA_T type structure. + pSinCos - Input - This parameter is a pointer to a MC_SINCOS_T type structure. + pDQ - Output - This parameter is a pointer to a MC_DQ_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ALPHABETA_T mcIAlphaBeta; + MC_SINCOS_T mcSinCos; + MC_DQ_T mcIDQ; + temp = MC_TransformPark_Assembly(&mcIAlphaBeta, &mcSinCos, &mcIDQ); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +uint16_t MC_ATTRB MC_TransformPark_Assembly( const MC_ALPHABETA_T *pAlphaBeta, + const MC_SINCOS_T *pSinCos, + MC_DQ_T *pDQ); + + +/******************************************************************************* + Function: + uint16_t MC_ControllerPIUpdate_Assembly(int16_t inReference, int16_t inMeasure, MC_PISTATE_T *pPIState, int16_t *pPIParmOutput) + + Summary: + This function calculates the PI correction. + + Description: + This function calculates a PI correction output from a given measured input and a reference. + The equation for PI output is: + + out = Kp*(inReference-inMeasure) + Ki*Integral[inReference-inMeasure, dt] - Kc*Excess + + Where, + out = Fractional 1.15 output, is limited to between outMax and outMin. + Kp = Proportional gain co-efficient term + Ki = Integral gain co-efficient term + Kc = Excess gain co-efficient term + Excess = Excess error after "out" is limited to between outMax and outMin. + This implementation includes an anti-windup term to limit the integral windup. + + Precondition: + None. + + Parameters: + inReference - Input - This parameter is a 1.15 fractional format reference input. + inMeasure - Input - This parameter is a 1.15 fractional format measured input. + pPIState - Input/Output - This paramater is a pointer to a MC_PISTATE_T type structure. + pPIParmOutput - Output - This paramater is a pointer to a signed integer type variable. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_PIPARMIN_T mcPIParmInput; + MC_PIPARMOUT_T mcPIParmOutput; + temp = MC_ControllerPIUpdate_Assembly(mcPIParmInput.inReference, mcPIParmInput.inMeasure, &mcPIParmInput.piState, &mcPIParmOutput.out); + + + Remarks: + This routine requires inputs in the 1.15 format, except for Kp which is in 1.11 format. + The constant Kp is scaled so it can be represented in 1.15 format by adjusting the constant + by a power of 2. +*******************************************************************************/ +uint16_t MC_ATTRB MC_ControllerPIUpdate_Assembly(int16_t inReference, + int16_t inMeasure, + MC_PISTATE_T *pPIState, + int16_t *pPIParmOutput); + +#ifdef __cplusplus // Provide C++ Compatibility + } +#endif +#endif // _MOTOR_CONTROL_DECLARATIONS_H_ + + + + + diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_dsp.h b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_dsp.h new file mode 100644 index 0000000..7fd6411 --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_dsp.h @@ -0,0 +1,78 @@ +/******************************************************************************* + Motor Control library -- DSP registers + + Company: + Microchip Technology Inc. + + File Name: + motor_control_dsp.h + + Summary: + DSP register declarations + + Description: + Contains DSP register declarations in global variables. +*******************************************************************************/ + +// DOM-IGNORE-BEGIN +/* ********************************************************************* + * + * (c) 2017 Microchip Technology Inc. and its subsidiaries. You may use + * this software and any derivatives exclusively with Microchip products. + * + * This software and any accompanying information is for suggestion only. + * It does not modify Microchip's standard warranty for its products. + * You agree that you are solely responsible for testing the software and + * determining its suitability. Microchip has no obligation to modify, + * test, certify, or support the software. + + * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, + * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, + * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, + * AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH + * MICROCHIP PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY + * APPLICATION. + + * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, + * PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF + * ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF + * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE + * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL + * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT + * EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO + * MICROCHIP FOR THIS SOFTWARE. + + * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF + * THESE TERMS. + * + * *****************************************************************************/ +// DOM-IGNORE-END + +#ifndef _MOTOR_CONTROL_DSP_H_ +#define _MOTOR_CONTROL_DSP_H_ + +#ifdef __cplusplus // Provide C++ Compatability + extern "C" { +#endif + +#ifndef DSP_ACCUMULATOR_A_DEFINED +#define DSP_ACCUMULATOR_A_DEFINED +/** DSP accumulator A */ +volatile register int a_Reg asm("A"); +#endif + +#ifndef DSP_ACCUMULATOR_B_DEFINED +#define DSP_ACCUMULATOR_B_DEFINED +/** DSP accumulator B */ +volatile register int b_Reg asm("B"); +#endif + +#ifdef __cplusplus // Provide C++ Compatibility + } +#endif + +#endif // _MOTOR_CONTROL_DSP_H_ + + + + diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_inline_declarations.h b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_inline_declarations.h new file mode 100644 index 0000000..1bc9e4d --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_inline_declarations.h @@ -0,0 +1,571 @@ +/******************************************************************************* + Motor Control Library Inline Function Declaration Header File + + File Name: + motor_control_inline_declarations.h + + Summary: + This header file lists all the inline function declarations used by the Motor Control library. + + Description: + This header file lists all the inline function declarations used by the Motor Control library. +*******************************************************************************/ + +// DOM-IGNORE-BEGIN +/* ********************************************************************* + * + * (c) 2017 Microchip Technology Inc. and its subsidiaries. You may use + * this software and any derivatives exclusively with Microchip products. + * + * This software and any accompanying information is for suggestion only. + * It does not modify Microchip's standard warranty for its products. + * You agree that you are solely responsible for testing the software and + * determining its suitability. Microchip has no obligation to modify, + * test, certify, or support the software. + + * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, + * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, + * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, + * AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH + * MICROCHIP PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY + * APPLICATION. + + * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, + * PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF + * ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF + * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE + * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL + * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT + * EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO + * MICROCHIP FOR THIS SOFTWARE. + + * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF + * THESE TERMS. + * + * *****************************************************************************/ +/******************************************************************************* +Note: +* Some of the function declarations have a MC_ATTRB_INLINE prefix. This prefix has been + provided as a placeholder for adding attributes for supporting future versions + of the compiler. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef _MOTOR_CONTROL_INLINE_DECLARATIONS_H_ // Guards against multiple inclusion +#define _MOTOR_CONTROL_INLINE_DECLARATIONS_H_ + +// ***************************************************************************** +// ***************************************************************************** +// Section: Included Files +// ***************************************************************************** +// ***************************************************************************** + +#include +#include "motor_control_types.h" + +#if __XC16_VERSION__ > 1011 // Place-holder function attribute prefix +#define MC_ATTRB_INLINE +#else +#define MC_ATTRB_INLINE +#endif + +#ifdef __cplusplus // Provide C++ Compatability + extern "C" { +#endif + +// ***************************************************************************** +// ***************************************************************************** +// Section: Interface Routines +// ***************************************************************************** +// ***************************************************************************** + + + +/******************************************************************************* + Function: + static inline uint16_t MC_CalculateSineCosine_InlineC_Ram(int16_t angle, MC_SINCOS_T *pSinCos) + + Summary: + This function calculates the Sine and Cosine values for a specified angle input. + + Description: + This function calculates the Sine and Cosine values for specified angle input using + linear interpolation on a sine table of 128 words. This routine works the same for + both integer input and 1.15 scaling input. + + Precondition: + None. + + Parameters: + angle - Input - This parameter is the input angle which will be used to calculate + the Sine and Cosine components. + pSinCos - Output - This parameter is a pointer to a MC_SINCOS_T type structure to + which the Sine and Cosine components of the angle are written. + + Returns: + Unsigned integer value '1' for direct look up and '2' for interpolation. + + Example: + + uint16_t temp; + int16_t angle; + MC_SINCOS_T mcSinCos; + temp = MC_CalculateSineCosine_InlineC_Ram(angle, &mcSinCos); + + + Remarks: + This function uses the sine-table included within the library archive file. Hence, + the library archive file must be included in the application project for this function + to link correctly. + + For integer scaling the Angle is scaled such that 0 <= Angle < 2*pi corresponds + to 0 <= Ang < 0xFFFF. The resulting Sine and Cosine values are returned scaled to + -32769 -> 32767 i.e. (0x8000 -> 0x7FFF). + For 1.15 scaling the Angle is scaled such that -pi <= Angle < pi corresponds to + -1 -> 0.9999 i.e. (0x8000 <= Ang < 0x7FFF). The resulting Sine and Cosine values are + returned scaled to -1 -> 0.9999 i.e. (0x8000 -> 0x7FFF). + *******************************************************************************/ +static inline uint16_t MC_ATTRB_INLINE MC_CalculateSineCosine_InlineC_Ram(int16_t angle, + MC_SINCOS_T *pSinCos); + +/******************************************************************************* + Function: + static inline uint16_t MC_TransformClarkeInverseSwappedInput_InlineC( const MC_ALPHABETA_T *pAlphaBeta, MC_ABC_T *pABC) + + Summary: + This function calculates the scaled reference vectors using inputs in an alpha-beta reference frame. + + Description: + This function calculates the scaled reference vectors in an a-b-c reference frame using + inputs from an alpha-beta reference frame, as described by the equation: + + a = beta + b = -beta/2 + (sqrt(3)/2) * alpha + c = -beta/2 - (sqrt(3)/2) * alpha + + + This is a modified variant of the inverse Clarke transformation where alpha & beta are + swapped compared to the normal inverse Clarke transformation. This function is designed + to work with the CalculateSpaceVectorPhaseShifted() in order to simplify the calculation of + three-phase duty cycle values from a given set of inputs in the alpha-beta reference frame. + + Precondition: + None. + + Parameters: + pAlphaBeta - Input - This parameter is a pointer to a MC_ALPHABETA_T type structure. + pABC - Output - This parameter is a pointer to a MC_ABC_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ALPHABETA_T mcVAlphaBeta; + MC_ABC_T mcVabc; + temp = MC_TransformClarkeInverseSwappedInput_InlineC(&mcVAlphaBeta, &mcVabc); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +static inline uint16_t MC_ATTRB_INLINE MC_TransformClarkeInverseSwappedInput_InlineC( const MC_ALPHABETA_T *pAlphaBeta, + MC_ABC_T *pABC); + +/******************************************************************************* + Function: + static inline uint16_t MC_TransformClarkeInverse_InlineC( const MC_ALPHABETA_T *pAlphaBeta, MC_ABC_T *pABC) + + Summary: + This function calculates the scaled reference vectors using inputs in an alpha-beta reference frame. + + Description: + This function calculates the scaled reference vectors in an a-b-c reference frame using + inputs from an alpha-beta reference frame, as described by the equation: + + a = alpha + b = -alpha/2 + (sqrt(3)/2) * beta + c = -alpha/2 - (sqrt(3)/2) * beta + + + This is the conventional form of the inverse Clarke transformation. This function is designed + to work with the CalculateSpaceVector(). + + Precondition: + None. + + Parameters: + pAlphaBeta - Input - This parameter is a pointer to a MC_ALPHABETA_T type structure. + pABC - Output - This parameter is a pointer to a MC_ABC_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ALPHABETA_T mcVAlphaBeta; + MC_ABC_T mcVabc; + temp = MC_TransformClarkeInverse_InlineC(&mcVAlphaBeta, &mcVabc); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +static inline uint16_t MC_ATTRB_INLINE MC_TransformClarkeInverse_InlineC( const MC_ALPHABETA_T *pAlphaBeta, + MC_ABC_T *pABC); + +/******************************************************************************* + Function: + static inline void MC_TransformClarkeInverseNoAccum_InlineC( const MC_ALPHABETA_T *pAlphaBeta, MC_ABC_T *pABC) + + Summary: + This function calculates the scaled reference vectors using inputs in an alpha-beta reference frame. + + Description: + This function calculates the scaled reference vectors in an a-b-c reference frame using + inputs from an alpha-beta reference frame, as described by the equation: + + a = alpha + b = -alpha/2 + (sqrt(3)/2) * beta + c = -alpha/2 - (sqrt(3)/2) * beta + + + This is the conventional form of the inverse Clarke transformation. This function is designed + to work with the CalculateSpaceVector(). It does not use the DSP accumulators. + + Precondition: + None. + + Parameters: + pAlphaBeta - Input - This parameter is a pointer to a MC_ALPHABETA_T type structure. + pABC - Output - This parameter is a pointer to a MC_ABC_T type structure. + + Returns: + void + + Example: + + MC_ALPHABETA_T mcVAlphaBeta; + MC_ABC_T mcVabc; + MC_TransformClarkeInverseNoAccum_InlineC(&mcVAlphaBeta, &mcVabc); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +static inline void MC_ATTRB_INLINE MC_TransformClarkeInverseNoAccum_InlineC( const MC_ALPHABETA_T *pAlphaBeta, + MC_ABC_T *pABC); + +/******************************************************************************* + Function: + static inline uint16_t MC_CalculateSpaceVectorPhaseShifted_InlineC( const MC_ABC_T *pABC, + uint16_t iPwmPeriod, MC_DUTYCYCLEOUT_T *pDutyCycleOut) + + Summary: + This function calculates the duty cycle values based on the three scaled + reference vectors in the a-b-c reference frame and the PWM period value. + + Description: + This function calculates the duty cycle values based on the three scaled reference + vectors in the a-b-c reference frame and the PWM period value. + + This function is designed to work with the TransformClarkeInverseSwappedInput() + in order to simplify the calculation of three-phase duty cycle values from a given + set of inputs in the alpha-beta reference frame. + This function uses a reference axis that is phase shifted by 30 degrees relative + to the standard Space Vector Modulation reference axis. This phase-shifted reference + axis is accommodated by using reference vector inputs from a modified version of + the inverse Clarke transform which swaps the alpha-beta values at its input. + + Precondition: + None. + + Parameters: + pABC - Input - This parameter is a pointer to a MC_ABC_T type structure. + iPwmPeriod - Input - This parameter is an unsigned integer value of the PWM period. + pDutyCycleOut - Output - This parameter is a pointer to a MC_DUTYCYCLEOUT_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ABC_T mcVabc; + uint16_t iPwmPeriod; + MC_DUTYCYCLEOUT_T mcDutyCycleOut; + temp = MC_CalculateSpaceVectorPhaseShifted_InlineC(&mcVabc, iPwmPeriod, &mcDutyCycleOut); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +static inline uint16_t MC_ATTRB_INLINE MC_CalculateSpaceVectorPhaseShifted_InlineC( const MC_ABC_T *pABC, + uint16_t iPwmPeriod, + MC_DUTYCYCLEOUT_T *pDutyCycleOut); + +/******************************************************************************* + Function: + static inline void MC_CalculateZeroSequenceModulation_InlineC(const MC_ABC_T *pabc_in, MC_ABC_T *pabc_out, int16_t min, int16_t max) + + Summary: + Computes realizable duty cycles from a three-phase set of inputs + so that they are centered and clipped within a given output range.. + + Description: + Adds an identical zero-sequence offset to the input duty cycles to center them + within the requested output range, and clips the resulting outputs between min and max. + This is equivalent to conventional space vector PWM (CSVPWM). + + As long as clipping does not occur, differences between any pair of signals + are preserved from input to output, for a small-signal gain of 1. + + Precondition: + None. + + Parameters: + pabc_in - Input - This parameter is a pointer to a MC_ABC_T type structure. + pabc_out - Output - This pointer to structure returns the CSVPWM ZSM modulated signal. + min,max - Input - These parameters supply the min and max values to clip the duty cycle + limits to. + + Returns: + void + + Example: + + MC_ABC_T mcVabc; + MC_ABC_T mcVabcOut; + int16_t min,max; + + MC_CalculateZeroSequenceModulation_InlineC(&mcVabc, &mcVabcOut, min, max ); + + + Remarks: + In order to reduce execution time, the implementation of this function assumes + that the output values min and max must have an average which is not negative. + (Typically min is a number slightly above 0 and max is a number slightly below 1.0 Q15) + +*******************************************************************************/ +static inline void MC_ATTRB_INLINE MC_CalculateZeroSequenceModulation_InlineC(const MC_ABC_T *pabc_in, MC_ABC_T *pabc_out, int16_t min, int16_t max); + +/******************************************************************************* + Function: + static inline uint16_t MC_TransformClarke_InlineC( const MC_ABC_T *pABC, MC_ALPHABETA_T *pAlphaBeta) + + Summary: + This function calculates the Clarke transformation. + + Description: + This function transforms inputs in an a-b-c reference frame to an alpha-beta + reference frame using the equation: + + alpha = a + beta = a*(1/sqrt(3)) + 2*b*(1/sqrt(3)) + + + Precondition: + None. + + Parameters: + pABC - Input - This parameter is a pointer to a MC_ABC_T type structure. + pAlphaBeta - Output - This parameter is a pointer to a MC_ALPHABETA_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ABC_T mcIabc; + MC_ALPHABETA_T mcIAlphaBeta; + temp = MC_TransformClarke_InlineC(&mcIabc, &mcIAlphaBeta); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +static inline uint16_t MC_ATTRB_INLINE MC_TransformClarke_InlineC( const MC_ABC_T *pABC, + MC_ALPHABETA_T *pAlphaBeta); + +/******************************************************************************* + Function: + static inline void MC_TransformClarkeABC_InlineC( const MC_ABC_T *pABC, MC_ALPHABETA_T *pAlphaBeta) + + Summary: + This function calculates the Clarke transformation. + + Description: + This function transforms inputs from per-phase a-b-c reference frame to orthogonal alpha-beta + reference frame given by + + alpha = a*2/3 - b/3- c/3 + beta = b/sqrt3 - c/sqrt3 + + This function differs from the more popular version that transforms from a-b to alpha-beta + exploiting the identity a + b + c = 0 in a system with no neutral current flow. + + Precondition: + None. + + Parameters: + pABC - Input - pointer to a MC_ABC_T type structure. + pAlphaBeta - Output - pointer to a MC_ALPHABETA_T type structure. + + Returns: + void + + Example: + + MC_ABC_T mcIabc; + MC_ALPHABETA_T mcIAlphaBeta; + MC_TransformClarkeABC_InlineC(&mcIabc, &mcIAlphaBeta); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +static inline void MC_ATTRB_INLINE MC_TransformClarkeABC_InlineC( const MC_ABC_T *abc, MC_ALPHABETA_T *alphabeta); + +/******************************************************************************* + Function: + static inline uint16_t MC_TransformPark_InlineC( const MC_ALPHABETA_T *pAlphaBeta, + const MC_SINCOS_T *pSinCos, MC_DQ_T *pDQ) + + Summary: + This function calculates the Park transformation. + + Description: + This function transforms inputs in an alpha-beta reference frame to a stationary + d-q reference frame using the equation: + + d = alpha*cos + beta*sin + q = -alpha*sin + beta*cos + + + Precondition: + None. + + Parameters: + pAlphaBeta - Input - This parameter is a pointer to a MC_ALPHABETA_T type structure. + pSinCos - Input - This parameter is a pointer to a MC_SINCOS_T type structure. + pDQ - Output - This parameter is a pointer to a MC_DQ_T type structure. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ALPHABETA_T mcIAlphaBeta; + MC_SINCOS_T mcSinCos; + MC_DQ_T mcIDQ; + temp = MC_TransformPark_InlineC(&mcIAlphaBeta, &mcSinCos, &mcIDQ); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +static inline uint16_t MC_ATTRB_INLINE MC_TransformPark_InlineC(const MC_ALPHABETA_T *alphabeta, + const MC_SINCOS_T *sincos, + MC_DQ_T *dq); + +/******************************************************************************* + Function: + static inline uint16_t MC_TransformParkInverse_InlineC(const MC_DQ_T *dq, const MC_SINCOS_T *sincos, + MC_ALPHABETA_T *alphabeta) + + Summary: + This function calculates the Inverse Park transformation. + + Description: + This function transforms inputs in stationary d-q reference frame to a rotating + alpha-beta reference frame using the equation: + + + alpha = d*cos - q*sin + beta = d*sin + q*cos + + + Precondition: + None. + + Parameters: + pDQ - Output - This parameter is a pointer to a MC_DQ_T type structure. + pSinCos - Input - This parameter is a pointer to a MC_SINCOS_T type structure. + pAlphaBeta - Input - This parameter is a pointer to a MC_ALPHABETA_T type structure. + + + Returns: + Unsigned integer value '1'. + + Example: + + MC_ALPHABETA_T mcIAlphaBeta; + MC_SINCOS_T mcSinCos; + MC_DQ_T mcIDQ; + temp = MC_TransformParkInverse_InlineC(&mcIAlphaBeta, &mcSinCos, &mcIDQ); + + + Remarks: + This routine works for any q format so long as it is consistent across input and output. +*******************************************************************************/ +static inline uint16_t MC_ATTRB_INLINE MC_TransformParkInverse_InlineC(const MC_DQ_T *dq, const MC_SINCOS_T *sincos, + MC_ALPHABETA_T *alphabeta); + +/******************************************************************************* + Function: + static inline uint16_t MC_ControllerPIUpdate_InlineC(int16_t inReference, int16_t inMeasure, MC_PISTATE_T *pPIState, int16_t *pPIParmOutput) + + Summary: + This function calculates the PI correction. + + Description: + This function calculates a PI correction output from a given measured input and a reference. + The equation for PI output is: + + out = Kp*(inReference-inMeasure) + Ki*Integral[inReference-inMeasure, dt] - Kc*Excess + + Where, + out = Fractional 1.15 output, is limited to between outMax and outMin. + Kp = Proportional gain co-efficient term + Ki = Integral gain co-efficient term + Kc = Excess gain co-efficient term + Excess = Excess error after "out" is limited to between outMax and outMin. + This implementation includes an anti-windup term to limit the integral windup. + + Precondition: + None. + + Parameters: + inReference - Input - This parameter is a 1.15 fractional format reference input. + inMeasure - Input - This parameter is a 1.15 fractional format measured input. + pPIState - Input/Output - This paramater is a pointer to a MC_PISTATE_T type structure. + pPIParmOutput - Output - This paramater is a pointer to a signed integer type variable. + + Returns: + Unsigned integer value '1'. + + Example: + + MC_PIPARMIN_T mcPIParmInput; + MC_PIPARMOUT_T mcPIParmOutput; + temp = MC_ControllerPIUpdate_InlineC(mcPIParmInput.inReference, mcPIParmInput.inMeasure, &mcPIParmInput.piState, &mcPIParmOutput.out); + + + Remarks: + This routine requires inputs in the 1.15 format, except for Kp which is in 1.11 format. + The constant Kp is scaled so it can be represented in 1.15 format by adjusting the constant + by a power of 2. +*******************************************************************************/ +static inline uint16_t MC_ATTRB_INLINE MC_ControllerPIUpdate_InlineC(int16_t inReference, + int16_t inMeasure, + MC_PISTATE_T *pPIState, + int16_t *pPIParmOutput); + +#ifdef __cplusplus // Provide C++ Compatibility + } +#endif +#endif // _MOTOR_CONTROL_INLINE_DECLARATIONS_H_ + + + + + diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_inline_dspic.h b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_inline_dspic.h new file mode 100644 index 0000000..02871dd --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_inline_dspic.h @@ -0,0 +1,551 @@ +/******************************************************************************* + Motor Control library inline definitions header file + + Company: + Microchip Technology Inc. + + File Name: + motor_control_inline_dspic.h + + Summary: + This header file hosts inline definitions of certain library functions included + in the Motor Control library. + + Description: + This header file hosts inline definitions of certain library functions included + in the Motor Control library. This header file is automatically included when the + library interfaces header file is included in the project. +*******************************************************************************/ + +// DOM-IGNORE-BEGIN +/* ********************************************************************* + * + * (c) 2017 Microchip Technology Inc. and its subsidiaries. You may use + * this software and any derivatives exclusively with Microchip products. + * + * This software and any accompanying information is for suggestion only. + * It does not modify Microchip's standard warranty for its products. + * You agree that you are solely responsible for testing the software and + * determining its suitability. Microchip has no obligation to modify, + * test, certify, or support the software. + + * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, + * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, + * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, + * AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH + * MICROCHIP PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY + * APPLICATION. + + * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, + * PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF + * ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF + * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE + * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL + * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT + * EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO + * MICROCHIP FOR THIS SOFTWARE. + + * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF + * THESE TERMS. + * + * *****************************************************************************/ +// DOM-IGNORE-END + +#ifndef _MOTOR_CONTROL_INLINE_DSPIC_H_ +#define _MOTOR_CONTROL_INLINE_DSPIC_H_ + +// ***************************************************************************** +// ***************************************************************************** +// Section: Included Files +// ***************************************************************************** +// ***************************************************************************** +/* This section lists the other files that are included in this file. +*/ +#include +#include +#include "motor_control_util.h" +#include "motor_control_inline_internal.h" + +// Declarations for DSP registers +#include "motor_control_dsp.h" + +#ifdef __cplusplus // Provide C++ Compatability + extern "C" { +#endif + +#ifdef __dsPIC33F__ +__psv__ extern uint16_t MC_SineTableInFlash[] __attribute__((space(psv))); +#elif defined(__dsPIC33E__) || defined(__dsPIC33C__) +__eds__ extern uint16_t MC_SineTableInFlash[] __attribute__((space(psv))); +#else +#error The selected device is not compatible with the Motor Control library! +#endif + +extern uint16_t MC_SineTableInRam[]; + +enum { + /** CORCON bit definitions */ + MC_LATENCY_CONTROL = 0x8000, + MC_UNSIGNED_MULTIPLY = 0x1000, + MC_MIXED_MULTPLY = 0x2000, + MC_DO_lOOP_TERMINATION = 0x800, + MC_DO_LOOP_7 = 0x700, + MC_DO_LOOP_6 = 0x600, + MC_DO_LOOP_5 = 0x500, + MC_DO_LOOP_4 = 0x400, + MC_DO_LOOP_3 = 0x300, + MC_DO_LOOP_2 = 0x200, + MC_DO_LOOP_1 = 0x100, + MC_SAT_ACCA_ENABLE = 0x80, + MC_SAT_ACCB_ENABLE = 0x40, + MC_SAT_DATASPACE = 0x20, + MC_SAT_SUPER_MODE = 0x10, + MC_CPU_HIGH_PRIORITY = 0x8, + MC_STACK_FRAME_ACTIVE = 0x4, + MC_ROUND_MODE_BIASED = 0x2, + MC_INTEGER_MODE = 0x1, +}; + +enum { + /** saturation enabled for ACCA and ACCB, DSP to data space write enabled, Biased rounding mode */ + MC_CORECONTROL = ( MC_SAT_ACCA_ENABLE | MC_SAT_ACCB_ENABLE | MC_SAT_DATASPACE | MC_ROUND_MODE_BIASED ) +}; +// ***************************************************************************** +// ***************************************************************************** +// Section: Inline function definitions +// ***************************************************************************** +// ***************************************************************************** +/* This section lists inline implementation of the library functions. +*/ + +static inline uint16_t MC_TransformPark_InlineC( const MC_ALPHABETA_T *alphabeta, const MC_SINCOS_T *sincos, MC_DQ_T *dq) +{ + uint16_t mcCorconSave = CORCON; + CORCON = MC_CORECONTROL; + + /* Id = Ialpha*cos(Angle) + Ibeta*sin(Angle) */ + a_Reg = __builtin_mpy(alphabeta->alpha, sincos->cos,0,0,0,0,0,0); + a_Reg = __builtin_mac(a_Reg, alphabeta->beta, sincos->sin,0,0,0,0,0,0,0,0); + dq->d = __builtin_sacr(a_Reg,0); + + /* Iq = - Ialpha*sin(Angle) + Ibeta*cos(Angle) */ + a_Reg = __builtin_mpy(alphabeta->beta, sincos->cos,0,0,0,0,0,0); + a_Reg = __builtin_msc(a_Reg, alphabeta->alpha, sincos->sin,0,0,0,0,0,0,0,0); + dq->q = __builtin_sacr(a_Reg,0); + + CORCON = mcCorconSave; + return 1; +} + + +static inline uint16_t MC_ControllerPIUpdate_InlineC(int16_t in_Ref, int16_t in_Meas, MC_PISTATE_T *state, int16_t *out) +{ + int16_t error; + int16_t out_Buffer; + int16_t output; + + uint16_t mcCorconSave = CORCON; + CORCON = MC_CORECONTROL; + + /* Calculate error */ + a_Reg = __builtin_lac(in_Ref,0); + b_Reg = __builtin_lac(in_Meas,0); + a_Reg = __builtin_subab(a_Reg,b_Reg); + error = __builtin_sacr(a_Reg,0); + + /* Read state->integrator into B */ + MC_UTIL_writeAccB32(state->integrator); + + /* Calculate (Kp * error * 2^4), store in A and out_Buffer */ + a_Reg = __builtin_mpy(error, state->kp,0,0,0,0,0,0); + a_Reg = __builtin_sftac(a_Reg,-4); + a_Reg = __builtin_addab(a_Reg,b_Reg); + out_Buffer = __builtin_sacr(a_Reg,0); + + /* Limit the output */ + if(out_Buffer > state->outMax) + { + output = state->outMax; + } + else if(out_Buffer < state->outMin) + { + output = state->outMin; + } + else + { + output = out_Buffer; + } + *out = output; + + /* Calculate (error * Ki) and store in A */ + a_Reg = __builtin_mpy(error, state->ki,0,0,0,0,0,0); + + /* Calculate (excess * Kc), subtract from (error * Ki) and store in A */ + error = out_Buffer - output; + a_Reg = __builtin_msc(a_Reg, error, state->kc,0,0,0,0,0,0,0,0); + + /* Add (error * Ki)-(excess * Kc) to the integrator value in B */ + a_Reg = __builtin_addab(a_Reg,b_Reg); + asm volatile ("" : "+w"(a_Reg):); // Prevent optimization from re-ordering/ignoring this sequence of operations + + /* Store the integrator result */ + state->integrator = MC_UTIL_readAccA32(); + CORCON = mcCorconSave; + + return 1; +} + + +static inline uint16_t MC_TransformClarke_InlineC( const MC_ABC_T *abc, MC_ALPHABETA_T *alphabeta) +{ + const uint16_t MC_ONEBYSQ3 = 18919u; + uint16_t mcCorconSave = CORCON; + CORCON = MC_CORECONTROL; + + /* alpha = a */ + alphabeta->alpha = abc->a; + + /* beta = a/sqrt(3) + 2*b/sqrt(3) */ + a_Reg = __builtin_mpy(abc->a, MC_ONEBYSQ3,0,0,0,0,0,0); + a_Reg = __builtin_mac(a_Reg, MC_ONEBYSQ3, abc->b,0,0,0,0,0,0,0,0); + a_Reg = __builtin_mac(a_Reg, MC_ONEBYSQ3, abc->b,0,0,0,0,0,0,0,0); + alphabeta->beta = __builtin_sacr(a_Reg,0); + + CORCON = mcCorconSave; + return 1; +} + + +static inline void MC_TransformClarkeABC_InlineC(const MC_ABC_T *abc, MC_ALPHABETA_T *alphabeta) +{ + const int16_t tan30Q16 = 37837U; // 1/sqrt(3) Q16 + const int16_t one_thirdQ16 = 21845U; // 1/3 Q16 + const int16_t two_thirdsQ16 = 43690U; // 2/3 Q16 + /* note that 43691 would be more accurate but this maintains equal gain among the phases */ + + /* alpha = a*2/3 - b/3- c/3 */ + alphabeta->alpha = MC_UTIL_mulus16(two_thirdsQ16, abc->a) + - MC_UTIL_mulus16(one_thirdQ16, abc->b) + - MC_UTIL_mulus16(one_thirdQ16, abc->c); + + /* beta = b/sqrt(3) - c/sqrt(3) */ + alphabeta->beta = MC_UTIL_mulus16(tan30Q16, abc->b) + - MC_UTIL_mulus16(tan30Q16, abc->c); +} + +static inline uint16_t MC_TransformParkInverse_InlineC(const MC_DQ_T *dq, const MC_SINCOS_T *sincos, MC_ALPHABETA_T *alphabeta) +{ + uint16_t mcCorconSave = CORCON; + CORCON = MC_CORECONTROL; + + /* alphabeta->alpha = (dq->d * sincos->cos) - (dq->q * sincos->sin) */ + a_Reg = __builtin_mpy(dq->d, sincos->cos,0,0,0,0,0,0); + a_Reg = __builtin_msc(a_Reg, dq->q, sincos->sin,0,0,0,0,0,0,0,0); + alphabeta->alpha = __builtin_sacr(a_Reg,0); + + /* alphabeta->beta = (dq->d * sincos->sin) + (dq->q * sincos->cos) */ + a_Reg = __builtin_mpy(dq->d, sincos->sin,0,0,0,0,0,0); + a_Reg = __builtin_mac(a_Reg, dq->q, sincos->cos,0,0,0,0,0,0,0,0); + alphabeta->beta = __builtin_sacr(a_Reg,0); + + CORCON = mcCorconSave; + return 1; +} + + +static inline uint16_t MC_TransformClarkeInverseSwappedInput_InlineC( const MC_ALPHABETA_T *alphabeta, MC_ABC_T *abc) +{ + const int16_t MC_SQ3OV2 = 28378u; + const int16_t MC_POINT5 = 0x4000; + uint16_t mcCorconSave = CORCON; + CORCON = MC_CORECONTROL; + + /* a = beta */ + abc->a = alphabeta->beta; + + /* b = (-beta/2) + (Sqrt(3)/2)*alpha */ + a_Reg = __builtin_clr(); + a_Reg = __builtin_msc(a_Reg, alphabeta->beta, MC_POINT5,0,0,0,0,0,0,0,0); + a_Reg = __builtin_mac(a_Reg, alphabeta->alpha, MC_SQ3OV2,0,0,0,0,0,0,0,0); + abc->b = __builtin_sacr(a_Reg,0); + + /* c = (-beta/2) - (Sqrt(3)/2)*alpha */ + a_Reg = __builtin_clr(); + a_Reg = __builtin_msc(a_Reg, alphabeta->beta, MC_POINT5,0,0,0,0,0,0,0,0); + a_Reg = __builtin_msc(a_Reg, alphabeta->alpha, MC_SQ3OV2,0,0,0,0,0,0,0,0); + abc->c = __builtin_sacr(a_Reg,0); + + CORCON = mcCorconSave; + return 1; +} + +static inline uint16_t MC_TransformClarkeInverse_InlineC( const MC_ALPHABETA_T *alphabeta, MC_ABC_T *abc) +{ + const int16_t MC_SQ3OV2 = 28378u; + const int16_t MC_NEGPOINT5 = 0xC000; + uint16_t mcCorconSave = CORCON; + CORCON = MC_CORECONTROL; + + /* a = alpha */ + abc->a = alphabeta->alpha; + + /* b = (-alpha/2) + (Sqrt(3)/2)*beta */ + a_Reg = __builtin_mpy(alphabeta->alpha, MC_NEGPOINT5,0,0,0,0,0,0); + a_Reg = __builtin_mac(a_Reg, alphabeta->beta, MC_SQ3OV2,0,0,0,0,0,0,0,0); + abc->b = __builtin_sacr(a_Reg,0); + + /* c = (-alpha/2) - (Sqrt(3)/2)*beta */ + a_Reg = __builtin_mpy(alphabeta->alpha, MC_NEGPOINT5,0,0,0,0,0,0); + a_Reg = __builtin_msc(a_Reg, alphabeta->beta, MC_SQ3OV2,0,0,0,0,0,0,0,0); + abc->c = __builtin_sacr(a_Reg,0); + + CORCON = mcCorconSave; + return 1; +} + +static inline void MC_TransformClarkeInverseNoAccum_InlineC( const MC_ALPHABETA_T *alphabeta, MC_ABC_T *abc) +{ + /* cos 30 deg = sqrt(3)/2 */ + const uint16_t cos30Q16 = 56756u; + + const int16_t alpha_sin30 = alphabeta->alpha >> 1; + const int16_t beta_cos30 = MC_UTIL_mulus16(cos30Q16, alphabeta->beta); + + /* a = alpha */ + abc->a = alphabeta->alpha; + /* b = -(alpha/2) + (Sqrt(3)/2)*beta */ + abc->b = -alpha_sin30 + beta_cos30; + /* c = -(alpha/2) - (Sqrt(3)/2)*beta */ + abc->c = -alpha_sin30 - beta_cos30; +} + +static inline uint16_t MC_CalculateSpaceVectorPhaseShifted_InlineC( const MC_ABC_T *abc, uint16_t period, MC_DUTYCYCLEOUT_T *pdcout) +{ + int16_t T1, T2, Ta, Tb, Tc; + + uint16_t mcCorconSave = CORCON; + CORCON = MC_CORECONTROL; + + if (abc->a >= 0) + { + // (xx1) + if (abc->b >= 0) + { + // (x11) + // Must be Sector 3 since Sector 7 not allowed + // Sector 3: (0,1,1) 0-60 degrees + T1 = abc->a; + T2 = abc->b; + /* T1 = period * T1 */ + a_Reg = __builtin_mulus(period, T1); + T1 = __builtin_sacr(a_Reg,0); + /* T2 = period * T2 */ + a_Reg = __builtin_mulus(period, T2); + T2 = __builtin_sacr(a_Reg,0); + Tc = period-T1-T2; + Tc = Tc >> 1; + Tb = Tc + T1; + Ta = Tb + T2; + pdcout->dutycycle1 = Ta; + pdcout->dutycycle2 = Tb; + pdcout->dutycycle3 = Tc; + } + else + { + // (x01) + if (abc->c >= 0) + { + // Sector 5: (1,0,1) 120-180 degrees + T1 = abc->c; + T2 = abc->a; + /* T1 = period * T1 */ + a_Reg = __builtin_mulus(period, T1); + T1 = __builtin_sacr(a_Reg,0); + /* T2 = period * T2 */ + a_Reg = __builtin_mulus(period, T2); + T2 = __builtin_sacr(a_Reg,0); + Tc = period-T1-T2; + Tc = Tc >> 1; + Tb = Tc + T1; + Ta = Tb + T2; + pdcout->dutycycle1 = Tc; + pdcout->dutycycle2 = Ta; + pdcout->dutycycle3 = Tb; + } + else + { + // Sector 1: (0,0,1) 60-120 degrees + T1 = -abc->c; + T2 = -abc->b; + /* T1 = period * T1 */ + a_Reg = __builtin_mulus(period, T1); + T1 = __builtin_sacr(a_Reg,0); + /* T2 = period * T2 */ + a_Reg = __builtin_mulus(period, T2); + T2 = __builtin_sacr(a_Reg,0); + Tc = period-T1-T2; + Tc = Tc >> 1; + Tb = Tc + T1; + Ta = Tb + T2; + pdcout->dutycycle1 = Tb; + pdcout->dutycycle2 = Ta; + pdcout->dutycycle3 = Tc; + } + } + } + else + { + // (xx0) + if (abc->b >= 0) + { + // (x10) + if (abc->c >= 0) + { + // Sector 6: (1,1,0) 240-300 degrees + T1 = abc->b; + T2 = abc->c; + /* T1 = period * T1 */ + a_Reg = __builtin_mulus(period, T1); + T1 = __builtin_sacr(a_Reg,0); + /* T2 = period * T2 */ + a_Reg = __builtin_mulus(period, T2); + T2 = __builtin_sacr(a_Reg,0); + Tc = period-T1-T2; + Tc = Tc >> 1; + Tb = Tc + T1; + Ta = Tb + T2; + pdcout->dutycycle1 = Tb; + pdcout->dutycycle2 = Tc; + pdcout->dutycycle3 = Ta; + } + else + { + // Sector 2: (0,1,0) 300-0 degrees + T1 = -abc->a; + T2 = -abc->c; + /* T1 = period * T1 */ + a_Reg = __builtin_mulus(period, T1); + T1 = __builtin_sacr(a_Reg,0); + /* T2 = period * T2 */ + a_Reg = __builtin_mulus(period, T2); + T2 = __builtin_sacr(a_Reg,0); + Tc = period-T1-T2; + Tc = Tc >> 1; + Tb = Tc + T1; + Ta = Tb + T2; + pdcout->dutycycle1 = Ta; + pdcout->dutycycle2 = Tc; + pdcout->dutycycle3 = Tb; + } + } + else + { + // (x00) + // Must be Sector 4 since Sector 0 not allowed + // Sector 4: (1,0,0) 180-240 degrees + T1 = -abc->b; + T2 = -abc->a; + /* T1 = period * T1 */ + a_Reg = __builtin_mulus(period, T1); + T1 = __builtin_sacr(a_Reg,0); + /* T2 = period * T2 */ + a_Reg = __builtin_mulus(period, T2); + T2 = __builtin_sacr(a_Reg,0); + Tc = period-T1-T2; + Tc = Tc >> 1; + Tb = Tc + T1; + Ta = Tb + T2; + pdcout->dutycycle1 = Tc; + pdcout->dutycycle2 = Tb; + pdcout->dutycycle3 = Ta; + } + } + + CORCON = mcCorconSave; + return 1; +} + +static inline void MC_CalculateZeroSequenceModulation_InlineC(const MC_ABC_T *pabc_in, MC_ABC_T *pabc_out, int16_t min, int16_t max) +{ + const int16_t center_out = MC_UTIL_AverageS16(min, max); + const MC_minmax16_t minmax_in = MC_UTIL_MinMax3_S16(pabc_in->a, pabc_in->b, pabc_in->c); + const int16_t center_in = MC_UTIL_AverageS16(minmax_in.min, minmax_in.max); + pabc_out->a = MC_adjust_zero_sequence(pabc_in->a - center_in, center_out, min, max); + pabc_out->b = MC_adjust_zero_sequence(pabc_in->b - center_in, center_out, min, max); + pabc_out->c = MC_adjust_zero_sequence(pabc_in->c - center_in, center_out, min, max); +} + +static inline uint16_t MC_CalculateSineCosine_InlineC_Ram( int16_t angle, MC_SINCOS_T *sincos ) +{ + uint16_t remainder, index, y0, y1, delta, return_value; + uint32_t result; + + return_value = 0; + + /* Index = (Angle*128)/65536 */ + result = __builtin_muluu(128,angle); + index = result >> 16; + remainder = (uint16_t) result ; + + /* Check if interpolation is required or not */ + if(remainder == 0) + { + /* No interpolation required, use index only */ + sincos->sin = MC_SineTableInRam[index]; + index = index+32; + if (index > 127) + { + index = index - 128; + } + sincos->cos = MC_SineTableInRam[index]; + return_value = 1; + } + else + { + /* Interpolation required. Determine the delta between indexed value + * and the next value from the mcSineTableInRam and scale the remainder + * with delta to get the linear interpolated value. */ + + y0 = MC_SineTableInRam[index]; + index = index+1; + if (index > 127) + { + index = index - 128; + } + y1 = MC_SineTableInRam[index]; + delta = y1 - y0; + result = __builtin_mulus(remainder,delta); + sincos->sin = y0 + ( result >>16 ); + + /* Increment by 32 for cosine index. Increment by 31 here + * since index has already been incremented once. */ + index = index+31; + if (index > 127) + { + index = index - 128; + } + + y0 = MC_SineTableInRam[index]; + index = index+1; + if (index > 127) + { + index = index - 128; + } + y1 = MC_SineTableInRam[index]; + delta = y1 - y0; + result = __builtin_mulus(remainder,delta); + sincos->cos = y0 + ( result >>16); + return_value = 2; + } + return return_value ; +} + +#ifdef __cplusplus // Provide C++ Compatibility + } +#endif + +#endif // _MOTOR_CONTROL_INLINE_DSPIC_H_ + + + + diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_inline_internal.h b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_inline_internal.h new file mode 100644 index 0000000..ab1af55 --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_inline_internal.h @@ -0,0 +1,96 @@ +/******************************************************************************* + Header file for Motor Control library inline definitions of internal functions + + Company: + Microchip Technology Inc. + + File Name: + motor_control_inline_internal.h + + Summary: + This header file contains implementation details of the Motor Control Library + that are not part of its public interface and are subject to change. + Please use the functions and type definitions contained in other header files + of the Motor Control Library instead. + + Description: + This header file is automatically included when the library interfaces header file + is included in the project. +*******************************************************************************/ + +// DOM-IGNORE-BEGIN +/* ********************************************************************* + * + * (c) 2017 Microchip Technology Inc. and its subsidiaries. You may use + * this software and any derivatives exclusively with Microchip products. + * + * This software and any accompanying information is for suggestion only. + * It does not modify Microchip's standard warranty for its products. + * You agree that you are solely responsible for testing the software and + * determining its suitability. Microchip has no obligation to modify, + * test, certify, or support the software. + + * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, + * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, + * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, + * AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH + * MICROCHIP PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY + * APPLICATION. + + * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, + * PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF + * ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF + * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE + * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL + * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT + * EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO + * MICROCHIP FOR THIS SOFTWARE. + + * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF + * THESE TERMS. + * + * *****************************************************************************/ +// DOM-IGNORE-END + +#ifndef _MOTOR_CONTROL_INLINE_INTERNAL_H_ +#define _MOTOR_CONTROL_INLINE_INTERNAL_H_ + +// ***************************************************************************** +// ***************************************************************************** +// Section: Included Files +// ***************************************************************************** +// ***************************************************************************** +/* This section lists the other files that are included in this file. +*/ +#include + + +/** + * Compute limit(x + ofs_out, min, max) + * This optimized code works ONLY if ofs_out >= 0 + */ +static inline int16_t MC_adjust_zero_sequence(int16_t x, int16_t ofs_out, int16_t min, int16_t max) +{ + int16_t w; + asm volatile ( + " add %[ofs_out], %[x], %[w]\n" /* overflow is only positive */ + " cpslt %[min], %[w]\n" + " mov %[min], %[w]\n" /* if (w < min) { w = min; } */ + " btss SR, #2\n" /* if ofs_out > 0, OV = SR<2> indicates we need positive clipping */ + " cpslt %[w], %[max]\n" + " mov %[max], %[w]\n" + : [w]"=&r"(w) + : [x]"r"(x), + [ofs_out]"r"(ofs_out), + [min]"r"(min), + [max]"r"(max) + ); + return w; +} + +#ifdef __cplusplus // Provide C++ Compatability + extern "C" { +#endif + +#endif // _MOTOR_CONTROL_INLINE_INTERNAL_H_ + diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_types.h b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_types.h new file mode 100644 index 0000000..9ee7517 --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_types.h @@ -0,0 +1,248 @@ +/******************************************************************************* + Motor Control Library Types Header File + + File Name: + motor_control_types.h + + Summary: + This header file lists all the types used by the Motor Control library. + + Description: + This header file lists the type defines for structures used by the Motor + Control library. +*******************************************************************************/ + +// DOM-IGNORE-BEGIN +/* ********************************************************************* + * + * (c) 2017 Microchip Technology Inc. and its subsidiaries. You may use + * this software and any derivatives exclusively with Microchip products. + * + * This software and any accompanying information is for suggestion only. + * It does not modify Microchip's standard warranty for its products. + * You agree that you are solely responsible for testing the software and + * determining its suitability. Microchip has no obligation to modify, + * test, certify, or support the software. + + * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, + * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, + * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, + * AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH + * MICROCHIP PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY + * APPLICATION. + + * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, + * PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF + * ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF + * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE + * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL + * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT + * EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO + * MICROCHIP FOR THIS SOFTWARE. + + * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF + * THESE TERMS. + * + * *****************************************************************************/ +// DOM-IGNORE-END + +#ifndef _MOTOR_CONTROL_TYPES_H_ // Guards against multiple inclusion +#define _MOTOR_CONTROL_TYPES_H_ + +#include + +#ifdef __cplusplus // Provide C++ Compatability + extern "C" { +#endif + + +// ***************************************************************************** +// ***************************************************************************** +// Section: Data Types +// ***************************************************************************** +// ***************************************************************************** + +// ***************************************************************************** + +/** Min Max values stored together + + Description: + minimum and maximum values +*/ + typedef struct +{ + // minimum + int16_t min; + + // maximum + int16_t max; +} MC_minmax16_t; + +// ***************************************************************************** +/** Alpha-Beta reference frame data type + + Description: + Parameters related to Alpha-Beta reference frame. +*/ +typedef struct +{ + // Alpha component + int16_t alpha; + + // Beta component + int16_t beta; +} MC_ALPHABETA_T; + +// ***************************************************************************** +/** Sine-Cosine data type + + Description: + Parameters related to Sine and Cosine components of the motor angle. +*/ +typedef struct +{ + // Cosine component + int16_t cos; + + // Sine component + int16_t sin; +} MC_SINCOS_T; + +// ***************************************************************************** +/** D-Q reference frame data type + + Description: + Parameters related to D-Q reference frame. +*/ +typedef struct +{ + // D-axis component + int16_t d; + + // Q-axis component + int16_t q; +} MC_DQ_T; + +// ***************************************************************************** +/** Duty-cycle data type + + Description: + Parameters related to PWM module Duty Cycle values. +*/ +typedef struct +{ + // Duty cycle for phase #1 + uint16_t dutycycle1; + + // Duty cycle for phase #2 + uint16_t dutycycle2; + + // Duty cycle for phase #3 + uint16_t dutycycle3; +} MC_DUTYCYCLEOUT_T; + +// ***************************************************************************** +/** ABC reference frame data type + + Description: + Parameters related to ABC reference frame. +*/ +typedef struct +{ + // Phase A component + int16_t a; + + // Phase B component + int16_t b; + + // Phase C component + int16_t c; +} MC_ABC_T; + +// ***************************************************************************** +/** PI Controller State data type + + Description: + Parameters related to the PI Controller state. +*/ +typedef struct +{ + // Integrator sum + int32_t integrator; + + // Proportional gain co-efficient term + int16_t kp; + + // Integral gain co-efficient term + int16_t ki; + + // Excess gain co-efficient term + int16_t kc; + + // Maximum output limit + int16_t outMax; + + // Minimum output limit + int16_t outMin; +} MC_PISTATE_T; + +// ***************************************************************************** +/** PI Controller Input data type + + Summary: + PI Controller input type define + + Description: + Parameters related to the PI Controller input. PI + controller state is a part of the PI Controller input. +*/ +typedef struct +{ + // PI state as input parameter to the PI controller + MC_PISTATE_T piState; + + // Input reference to the PI controller + int16_t inReference; + + // Input measured value + int16_t inMeasure; +} MC_PIPARMIN_T; + +// ***************************************************************************** +/** PI Controller Output data type + + Description: + Parameters related to the PI Controller output. +*/ +typedef struct +{ + // Output of the PI controller + int16_t out; +} MC_PIPARMOUT_T; + +// ***************************************************************************** +/** SIN and DIFF table data type + + Description: + SIN and the successive difference for the interpolation points + for the lookup table version of the SINCOS function +*/ +typedef struct +{ + // offset into the table of SIN entries + int16_t offset; + + // stores the successive difference of SIN entries + int16_t diff; +}MC_SINDIFF_T; + + +#ifdef __cplusplus // Provide C++ Compatibility + } +#endif +#endif // _MOTOR_CONTROL_TYPES_H_ + + + + + diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_util.h b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_util.h new file mode 100644 index 0000000..56a8a66 --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-motor/motor_control_util.h @@ -0,0 +1,176 @@ +/* + * File: motor_control_util.h + * + * Utility routines for computation used only in Motor control library + * + */ + +/* ********************************************************************* + * + * (c) 2017 Microchip Technology Inc. and its subsidiaries. You may use + * this software and any derivatives exclusively with Microchip products. + * + * This software and any accompanying information is for suggestion only. + * It does not modify Microchip's standard warranty for its products. + * You agree that you are solely responsible for testing the software and + * determining its suitability. Microchip has no obligation to modify, + * test, certify, or support the software. + + * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, + * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, + * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, + * AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH + * MICROCHIP PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY + * APPLICATION. + + * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, + * PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF + * ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF + * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE + * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL + * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT + * EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO + * MICROCHIP FOR THIS SOFTWARE. + + * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF + * THESE TERMS. + * + * *****************************************************************************/ + +#ifndef _MOTOR_CONTROL_UTIL_H_ +#define _MOTOR_CONTROL_UTIL_H_ + +#include + +// Declarations for DSP registers +#include "motor_control_dsp.h" + +/* + * Access to accumulator registers is required + * only for XC16 1.25 and earlier; this causes + * a dependency on which is unnecessary + * for newer versions of the compiler. + */ +#if __XC16_VERSION__ < 1026 +#include +#endif + + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * Helper function to multiply an unsigned 16-bit quantity + * and a signed 16-bit quantity and shift-right by 16. + * (one of the inputs should be a Q16 fixed-point values, + * and the other one and the output have identical binary points, + * e.g. Q12 = Q16 * Q12, or Q12 = Q12 * Q16) + * + * @param a first input (unsigned) + * @param b second input (signed) + * @return (a*b)>>16 + */ +inline static int16_t MC_UTIL_mulus16(uint16_t a, int16_t b) +{ + return __builtin_mulus(a,b) >> 16; +} + +/** + * Compute the average of two int16_t values + * @param a first value + * @param b second value + * @return (a+b)/2 + */ + +inline static int16_t MC_UTIL_AverageS16(int16_t a, int16_t b) +{ + return (int16_t)((((int32_t)a) + b) >> 1); +} + +/** + * Compute the minimum and maximum of a set of three int16_t values + * @param a first value + * @param b second value + * @param c third value + * @return struct containing minimum and maximum value -- + * this is fairly unusual but it permits the compiler to + * optimize by placing in an appropriate pair + * of adjacent working registers. + */ +inline static MC_minmax16_t MC_UTIL_MinMax3_S16(int16_t a, int16_t b, int16_t c) +{ + /* Sort a,b,c */ + asm ( + " cpslt %[a], %[b]\n" + " exch %[a], %[b]\n" + " cpslt %[a], %[c]\n" + " exch %[a], %[c]\n" + " cpslt %[b], %[c]\n" + " exch %[b], %[c]\n" + : [a]"+r"(a), + [b]"+r"(b), + [c]"+r"(c) + ); + /* Now a <= b <= c */ + + MC_minmax16_t result; + result.min = a; + result.max = c; + return result; +} + +/** Read accumulator A */ +inline static int32_t MC_UTIL_readAccA32() +{ +#if __XC16_VERSION__ >= 1030 + return __builtin_sacd(a_Reg, 0); +#elif __XC16_VERSION__ >= 1026 + const int32_t tmp = __builtin_sacd(a_Reg, 0); + /* Prevent optimization from re-ordering/ignoring this sequence of operations */ + asm volatile (""); + return tmp; +#else + int32_t result; + /* Prevent optimization from re-ordering/ignoring this sequence of operations */ + asm volatile ("" : "+w"(a_Reg):); + result = ACCAH; + result <<= 16; + result |= ACCAL; + return result; +#endif +} + +/** Write accumulator B */ +inline static void MC_UTIL_writeAccB32(int32_t input) +{ +#if __XC16_VERSION__ >= 1030 + b_Reg = __builtin_lacd(input, 0); +#elif __XC16_VERSION__ >= 1026 + const int32_t tmp = input; + asm volatile ("" :: "r"(tmp)); + b_Reg = __builtin_lacd(tmp, 0); +#else + uint32_t temp_dword; + uint16_t temp_word; + temp_dword = 0xFFFF0000 & input; + temp_dword = temp_dword >> 16; + temp_word = (uint16_t)temp_dword; + b_Reg = __builtin_lac(temp_word, 0); + /* Prevent optimization from re-ordering/ignoring this sequence of operations */ + asm volatile ("" : "+w"(b_Reg):); + temp_word = (uint16_t)(0xFFFF & input); + ACCBL = temp_word; +#endif + +} + + + +#ifdef __cplusplus +} +#endif + +#endif /* _MOTOR_CONTROL_UTIL_H_ */ + diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-x2cscope/README.md b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-x2cscope/README.md new file mode 100644 index 0000000..2ba4bef --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-x2cscope/README.md @@ -0,0 +1,3 @@ +# X2C Scope library for dsPIC33 DSC +

+X2C-Scope is a virtual oscilloscope tool developed by Linz Center of Mechatronics which allows run-time debugging or monitoring of your embedded application in MPLAB X IDE. This tool allows you to “Watch” or “Plot” any global variable in your embedded application at run-time i.e. without halting your CPU.

\ No newline at end of file diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-x2cscope/X2CScope.h b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-x2cscope/X2CScope.h new file mode 100644 index 0000000..4bd58be --- /dev/null +++ b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-x2cscope/X2CScope.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2013, Linz Center of Mechatronics GmbH (LCM) http://www.lcm.at/ + * (c) 2016, Microchip Technology Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Linz Center of Mechatronics GmbH, Microchip + * Technology Inc nor the names of its contributors may be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL Linz Center of Mechatronics GmbH, MICROCHIP TECHNOLOGY INC + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __X2CSCOPE_H__ +#define __X2CSCOPE_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void X2CScope_Initialise(uint8_t* buffer, size_t buffer_size); +void X2CScope_Communicate(); +void X2CScope_Update(); +void X2CScope_HookUARTFunctions(void (*sendSerialFcnPntr)(uint8_t), uint8_t (*receiveSerialFcnPntr)(), uint8_t (*isReceiveDataAvailableFcnPntr)(), uint8_t (*isSendReadyFcnPntr)()); + +#ifdef __cplusplus +} +#endif + +#endif /* __X2CSCOPE_H__ */ diff --git a/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-x2cscope/libx2cscope-generic-dspic-elf.a b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-x2cscope/libx2cscope-generic-dspic-elf.a new file mode 100644 index 0000000..1f0f0ac Binary files /dev/null and b/mclv-48v-300w-an1292-dspic33ck256mc506/library/library-x2cscope/libx2cscope-generic-dspic-elf.a differ