/*******************************************************************************
Copyright (c) 2014 released Microchip Technology Inc. All rights reserved.

Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).

You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.

SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
#ifndef _USERPARMS_H
#define _USERPARMS_H

#ifdef __cplusplus  // Provide C++ Compatability
    extern "C" {
#endif
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include <stdint.h>
#ifdef __XC16__  // See comments at the top of this header file
    #include <xc.h>
#endif // __XC16__

// *****************************************************************************
// *****************************************************************************
// Section: Constants
// *****************************************************************************
// *****************************************************************************
/* Define MOTOR_A as 1 */
#define MOTOR_A		1
/* Define MOTOR_B as 2  */
#define MOTOR_B 	2               

/*************** PWM and Control Timing Parameters ****************************/
/* Specify PWM Frequency in Hertz */
#define PWMFREQUENCY_HZ         20000
/* Specify dead time in micro seconds */
#define DEADTIME_MICROSEC       1
/* Specify PWM Period in seconds, (1/ PWMFREQUENCY_HZ) */
#define LOOPTIME_SEC            0.00005

/*************** Real Time Data Monitor, RTDM *******************
   This definition enabled Real Time Data Monitor, UART interrupts
   to handle RTDM protocol, and array declarations for buffering
   information in real time
   Define this if a demo with DMCI is done. Start/stop of motor
   and speed variation is done with DMCI instead of push button
   and POT. Undefine "DMCI_DEMO" is user requires operating
   this application with no DMCI
   Real Time Debug Monitor enable - if activated, RTDM will allow real time
   control and monitoring of the embedded side from host DMCI interface */
/*
IMPORTANT:
___________________--__________--_____________________________________
Communication between the host computer and the target device (dsPIC33EP)
may fail due to noisy environment, real time specificity of the system
or other unpredictable causes.
Since the host computer is used to completely control the target device,
in such failure cases, use the RESET button on MCHV module to stop the
execution and stop the motor.
___________________--__________--_____________________________________

Short usage guide
(please consult the http://ww1.microchip.com/downloads/en/DeviceDoc/70567A.pdf
for detailed information on RTDM and its usage):
Open DMCI- Data Monitor and Control Interface window from the
MPLAB’s Tools tab. Using afferent open pictogram, load dmci_rtdm.dmci
setup file from the AN1292 project’s folder. Within the DMCI window’s
frame, the following controls are available:
1/Boolean DMCIFlags.StartStop – used for start/stop  the motor
2/ Slider SpeedReference – used for reference speed modification
3/ Boolean DMCIFlags.Recorder – used for triggering a new sample of
measured variables
4/ Slider SnapShotDelay – used for sample rate modification of
the measured variables
5/ Graph 1-4 – Graphs of the measured variables: A phase current,
q-axis current, estimated speed, estimated angle
Running the application note software will connect the target
MCHV board to the host DMCI application as specified in above indicated
User’s Guide.
Suggested usage guide:
1/ Run the program using Run button in Dynamic Data Input DMCI child window
– allow few seconds for communication protocol to be established without
issuing any other command
2/ Run DMCI tab – Remote communication menu, which should indicate
connection status as DETECTED (if it doesn’t, please refer to the
above indicated User guide for troubleshooting). Close the DMCI Remote
Communication Properties window by pressing OK acknowledging the
detected communication.
3/ Press Run Time Update button in Dynamic Data Input DMCI child window –
this will update the host DMCI GUI with the initial setup already existing
on target device
4/ Adjust the speed reference using the afferent Slider SpeedReference
to a value of roughly 5000 (please note that both positive and negative
speed references can be set, so bidirectional functioning is selected
by default with DMCI_DEMO)
5/ Press Start/Stop button, switching it from OFF state to ON state
using afferent Boolean DMCIFlags.StartStop button in Dynamic Data Input
DMCI child window –  immediately the motor will start running
6/ Press Boolean DMCIFlags.Recorder in in Dynamic Data Input DMCI child
window in order to trigger a sample of the measured variables(the button
should be in ON state)
7/ Press Run Time Update button in Dynamic Data Input DMCI child window
– this will update the host DMCI GUI with the snapped data in target device.
8/ Use Automated Event Control button to have steps 6/, 7/ executed
automatically at 1 second period
9/ Vary the reference speed, vary the snap shot delay, etc.
10/ Press Start/Stop button, switching it from ON state to OFF state
using afferent Boolean DMCIFlags.StartStop button in Dynamic Data Input
DMCI child window –  immediately the motor will stop running
*/

/* consult debug.c API for calling the specific RTDM functions:
DBG_Init(void)	- RTDM initialization
DBG_SyncComm(void) - RTDM syncrho target and host in pooling mode
DBG_SnapUpdate(void) - RTDM variables update
These functions are called in pmsm.c */

//#define RTDM

#ifdef RTDM
    /* Size in 16-bit Words of the snap; The value depends on the dsPIC mem */
    #define DATA_BUFFER_SIZE    50
    /* In number of PWM Interrupts */
    #define SNAPDELAY           5
    /* Specify the snap captures */
    #define SNAP1		tClarkCurrentA.alpha
    #define SNAP2		tClarkCurrentA.beta
    #define SNAP3		tClarkCurrentB.alpha
    #define SNAP4		tClarkCurrentB.beta
#endif
/****************************************************/

#ifdef __cplusplus  // Provide C++ Compatibility
    }
#endif
#endif      // end of USERPARAMS_H


/* EOF */