/**
 * @file bldc_scalar_defines.h
 * @brief Non-configurable defines
 * @date 2016-05-18
 *
 ***********************************************************************************************************************
 * BLDC_SCALAR_CONTROL v0.2.0 - BLDC motor control using block commutation
 * Copyright (c) 2015-2016, Infineon Technologies AG
 * 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 the copyright holders nor the names of its contributors may be used to endorse or promote
 *   products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE  FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * To improve the quality of the software, users are encouraged to share modifications, enhancements or bug fixes
 * with Infineon Technologies AG (dave@infineon.com).
 ***********************************************************************************************************************
 *
 * Change History
 * --------------
 *
 * 2016-05-18:
 *     - Initial version
 *
 * @endcond
 *
 */

/**
 * @addtogroup BLDC_SCALAR BLDC Motor Control
 * @{
 */

/**
 * @addtogroup Configuration
 * @brief Control algorithm and MCU peripheral configurations <br>
 *
 * @{
 */

/***********************************************************************************************************************
 * HEADER FILES
 **********************************************************************************************************************/
#ifndef POWER_TOOLS_KIT_DIRECT_FET_H_
#define POWER_TOOLS_KIT_DIRECT_FET_H_

/***********************************************************************************************************************
 * MACROS
 **********************************************************************************************************************/

#define ENABLE_UCPROBE_OSC						(1U)									//Enable/Disable oscilloscope feature in micrium

#define MOTOR_NO_LOAD_SPEED						(20000U)								/*!< No load speed of the motor in RPM */
#define MOTOR_POLE_PAIRS						(2U)         							/*!< Pole pairs */
#define MODULATION								(BLDC_SCALAR_PWM_HIGHSIDE_SYNCHRECTI)	/*!< Options:	BLDC_SCALAR_PWM_HIGHSIDE
																										BLDC_SCALAR_PWM_LOWSIDE
																										BLDC_SCALAR_PWM_HIGHSIDE_SYNCHRECTI */
#define PWM_FREQ								(10000.0F)								/*!< PWM switching frequency in Hz. Range: 1000 to 100000 Hz */
#define SYSTICK_PERIOD							(1000.0F)								/*!< Scheduler interrupt period in us. Range: 1000 to 10000 us */
#define ENABLE_RAMP								(0U)									/*!< Enable (1)/disable(0) linear ramp */
#define RAMP_UP_RATE							(200.0F)								/*!< Unit is rpm/s. Range: 1 to BLDC_SCALAR_MOTOR_NO_LOAD_SPEED*/
#define RAMP_DOWN_RATE							(200.0F)								/*!< Unit is rpm/s. Range: 1 to BLDC_SCALAR_MOTOR_NO_LOAD_SPEED*/
#define SPEED_KP								(20972U)   								/*!< Proportional gain of speed PI in Q15. Range: 0 to 32767*/
#define SPEED_KI								(2U)									/*!< Integral gain of speed PI in Q15. Range: 0 to 32767 */
#define ENABLE_STALL_DETECTION					(1U)      								/*!< Enable/disable stall protection */
#define STALL_DETECTION_TIME					(1000.0F)  								/*!< Detection time for stall protection in mSec*/
#define NOMINAL_DC_LINK_VOLT					(20.0F)     							/*!< DC link voltage */
#define RISING_DEAD_TIME						(2.0F)     								/*!< Dead time for rising edge in uSec*/
#define FALLING_DEAD_TIME						(2.0F)     								/*!< Dead time for rising edge in uSec*/
#define CURRENT_AMPLIFIER_OFFSET				(1.0F)       							/*!< Amplifier offset voltage  */
#define CURRENT_RSHUNT							(1.0F)      							/*!< Current amplifier shunt resistor value in mOhms */
#define CURRENT_AMPLIFIER_GAIN					(10.0F)        							/*!< Current amplifier gain */
#define VOLTAGE_DIVIDER_RATIO					(19.65F)      							/*!< Voltage divider ratio in % for DC link voltage measurement */
#define AMPLITUDE_HIGH_THRESHOLD				(95.0F)   								/*!< in %. Max amplitude threshold above which maximum duty cycle is applied to avoid bootstrap capacitor discharge.
																								Range: 95 to 100% */
#define HALL_PAT_A								(1U)      								/*!< (MSB)H3 H2 H1 (LSB)*/
#define HALL_PAT_B								(3U)       								/*!< (MSB)H3 H2 H1 (LSB)*/
#define HALL_PAT_C								(2U)       								/*!< (MSB)H3 H2 H1 (LSB)*/
#define HALL_PAT_D								(6U)       								/*!< (MSB)H3 H2 H1 (LSB)*/
#define HALL_PAT_E								(4U)       								/*!< (MSB)H3 H2 H1 (LSB)*/
#define HALL_PAT_F								(5U)       								/*!< (MSB)H3 H2 H1 (LSB)*/
#define MC_PAT_A								(WL_VH_UOFF)  							/*!< Phase pattern corresponding to MOTOR0_BLDC_SCALAR_HALL_PAT_A */
#define MC_PAT_B								(WOFF_VH_UL)  							/*!< Phase pattern corresponding to MOTOR0_BLDC_SCALAR_HALL_PAT_B */
#define MC_PAT_C								(WH_VOFF_UL)  							/*!< Phase pattern corresponding to MOTOR0_BLDC_SCALAR_HALL_PAT_C */
#define MC_PAT_D								(WH_VL_UOFF)  							/*!< Phase pattern corresponding to MOTOR0_BLDC_SCALAR_HALL_PAT_D */
#define MC_PAT_E								(WOFF_VL_UH)  							/*!< Phase pattern corresponding to MOTOR0_BLDC_SCALAR_HALL_PAT_E */
#define MC_PAT_F								(WL_VOFF_UH)  							/*!< Phase pattern corresponding to MOTOR0_BLDC_SCALAR_HALL_PAT_F */
#define BLANKING_TIME							(0.5F)        							/*!< Hall edge re-sampling time in uSec. Range: 0.5 to 5 uSec*/
#define PH_DELAY_TIME							(1.0F)     								/*!< Time delay to update multi-channel pattern in uSec. Range: 0.6 to 5.1 uSec.
                                                                        						Phase delay time should be greater than blanking time minimum by 1 clock cycle*/
#define MIN_ANALOG_INPUT_LOW_LIMIT				(5.0F)    								/*!< in % of the analog input value. Analog value is considered as 0 below this limit.
                                                                      	  	  	  	  			Range: 1 to 5% */
#define POTENTIOMETER_PT1_FILTER_ENABLE			(0U)     								/*!< Enable (1)/disable (0) SW filter on potentiometer values */
#define POTENTIOMETER_FILTER_TIMECONST			(0.001F)   				 				/* PT1 filter time constant in sec*/
#define ENABLE_HALL_LEARNING					(0U)    		 						/*!< Enable/disable hall pattern learning */
#define OPEN_LOOP_VOLTAGE						(10.0F)     							/*!< Open loop voltage to be applied in % with respect to MOTOR0_BLDC_SCALAR_NOMINAL_DC_LINK_VOLT.
																								Range: 1 to 25 %*/
#define OPEN_LOOP_SPEED							(5.0F)     								/*!< Speed to be applied in % with respect to BLDC_SCALAR_MOTOR_NO_LOAD_SPEED.
																								Range: 0.11 to 10 %*/
#define TEMEPRATURE_SHUTDOWN_THRESHOLD			80					// Temperature shutdown threshold in [degress Celsius].
#define TEMPERATURE_HYSTERESIS					50					// Temperature start-up threshold in [degress Celsius].

#define POWER_TOOL_DIRECTFET_LED_INDICATOR    	P0_4				// Indicator LED at Pin 0.4
#define POWER_TOOL_DIRECTFET_DIRCTION_PIN	  	P0_11				// Direction input pin 0.11
#define POWER_TOOL_DIRECTFET_SHUTDOWN_PIN		P0_5				// Direction input pin 0.5
#define POWER_TOOL_Pin_2_2						P2_2
#define POWER_TOOL_Pin_2_10						P2_10
/***********************************************************************************************************************
 * API Prototypes
 **********************************************************************************************************************/
void Power_Tools_DirectFET_Init(void);
void Power_Tools_DirectFET_MotorStart(void);
void Power_Tools_DirectFET_MotorStop(void);
void Power_Tools_DirectFET_DirectionControl(void);
void Power_Tools_DirectFET_TemperatureControl(void);
void Power_Tools_DirectFET_FaultStatus(void);
#endif /* POWER_TOOLS_KIT_DIRECT_FET_H_ */
