/*******************************************************************************
 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).                                                          **
                                                                              **
********************************************************************************
**                                                                            **
**                                                                            **
** PLATFORM : Infineon XMC1300 Series                                         **
**                                                                            **
** AUTHOR : Motor Control  R&D Team                                           **
**                                                                            **
** version 1.0.0  Initial version                                             **
**                                                                            **
** MODIFICATION DATE : May 25, 2016                                           **
**                                                                            **
*******************************************************************************/

/***********************************************************************************************************************
 * HEADER FILES
 **********************************************************************************************************************/

#include "xmc_common.h"
#include "bldc_scalar_user_interface.h"
#include "../Power_Tools_Kit_DirectFET/power_tools_kit_DirectFET.h"
#if (ENABLE_UCPROBE_OSC ==1)
#include "../ProbeScope/probe_scope_cfg.h"
#include "../ProbeScope/probe_scope.h"
#endif
volatile uint32_t Control_Word=0;
volatile uint32_t  MaxSpeed ;
/**
@brief
This example project controls 3-phase BLDC motor with 3 hall sensor feedback using block commutation control algorithm.
This is configured for Infineon low voltage motor control kit KIT_XMC1X_AK_MOTOR_001 with Maxon motor part number 267121

A list of APIs is provided to control the motor and change the configurations
 - Motor0_BLDC_SCALAR_MotorStart
 - Motor0_BLDC_SCALAR_MotorStop
 Below APIs are for speed control. Similar APIs are available for voltage control and current control.
 - BLDC_SCALAR_SetSpeedVal
 - BLDC_SCALAR_SetSpeedProportionalGain
 - BLDC_SCALAR_SetSpeedIntegralGain
 */

/* Default configuration:
 * Infineon low voltage motor control kit KIT_XMC1X_AK_MOTOR_001 with Maxon motor
 * Control scheme: BLDC_SCALAR_SPEED_CTRL
 * PWM modulation: BLDC_SCALAR_PWM_HIGHSIDE
 * PWM frequency: 20000Hz
 * Speed set: 2000 RPM
 * Ramp up and ramp down rate: 200 RPM/s
 * Over-current protection with direct DC link current measurement
 */

/* Change of configurations:
 * Control algorithm configurations are in the Configuration/bldc_scalar_user_config.h file.
 * MCU HW resources configurations are in the Configuration/bldc_scalar_mcuhw_config.h file.
 */
#if (MOTOR0_BLDC_SCALAR_HALL_LEARNING == 1U)
volatile uint16_t MC_Pattern_DEBUG[7];
volatile uint16_t i=0;
#endif

int main(void)
{
  /* Initialization */
  Power_Tools_DirectFET_Init();
  MaxSpeed = MOTOR0_BLDC_SCALAR_MOTOR_NO_LOAD_SPEED;
#if (ENABLE_UCPROBE_OSC ==1)
  ProbeScope_Init(20000);
#endif
  /* Start the motor */
  Power_Tools_DirectFET_MotorStart();


#if (MOTOR0_BLDC_SCALAR_HALL_LEARNING == 1U)
  /* wait until hall pattern learning*/
  while (((uint8_t)BLDC_SCALAR_HALL_LEARNING_FLAG_DISABLED != Motor0_BLDC_SCALAR_Hall.hall_learning_flag))
  {

  }
  /*save the Hall sequence*/
  MC_Pattern_DEBUG[0]=Motor0_BLDC_SCALAR_HallLearning.closedloop_mc_pattern[0];

  /* Acquisition of Multi-Channel Pattern: save the commutation pattern in MC_Pattern_DEBUG variable*/
  for(i = 1; i <= 6; i++)
  {
	  MC_Pattern_DEBUG[i] = Motor0_BLDC_SCALAR_HallLearning.closedloop_mc_pattern[i];
  }

  while (1U)
  {
	  Power_Tools_DirectFET_MotorStop();
	  Control_Word=0;
  }

#endif

  /* Placeholder for user application code. The while loop below can be replaced with user application code. */
  while (1U)
  {

	  if(Control_Word==1)
	  	  {
	  		  /* Start the motor */
		  Power_Tools_DirectFET_MotorStart();
	  		  Control_Word=0;
	  	  }
	  	  else if(Control_Word==2)
	  	  {
	  		  /* Start the motor */
	  		Power_Tools_DirectFET_MotorStop();
	  		  Control_Word=0;
	  	  }
	  	  else if(Control_Word==3)
	  	  {
	  		  /* Start the motor */
	  		  Motor0_BLDC_SCALAR_ClearErrorState();
	  		  Control_Word=0;
	  	  }
  }

}
