/**
 * @file
 * @brief Collection of globally needed defines.
 *
 * Available Preprocessor Definitions:
 * - none
 * .
 */
/*
 * $LastChangedRevision: 3952 $
 * $LastChangedDate:: 2025-11-27 21:04:55 +0100#$
 *
 * This file is part of X2C. web: x2c.lcm.at
 *
 * Copyright (c) 2019, Linz Center of Mechatronics GmbH (LCM), web: x2c.lcm.at
 * All rights reserved.
 */
/*
 * This file is licensed according to the BSD 3-clause license as follows:
 *
 * 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 "Linz Center of Mechatronics GmbH" and "LCM" 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" 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 GLOBALDEFINES_H
    #define GLOBALDEFINES_H

    #ifdef __cplusplus
    extern "C" {
    #endif

    #include "TableStruct.h"
    #include "Target.h"
    #include "X2C.h"
    #include <xc.h>

	/*------------------------------------------------------------------------*/
	/* Watchdog */
	/*------------------------------------------------------------------------*/
    #define KICK_DOG \
        do { \
            ClrWdt(); \
            WDTCONH = 0x5743;\
        } while (0)


	/*------------------------------------------------------------------------*/
	/* X2C Outports */
	/*------------------------------------------------------------------------*/
	#define PWM1		(*x2cModel.outports.bPWM1)		/* PWM output 1 */
	#define PWM2		(*x2cModel.outports.bPWM2)		/* PWM output 2 */
	#define PWM3		(*x2cModel.outports.bPWM3)		/* PWM output 3 */
    #define LED_D2		(*x2cModel.outports.bLED2)      /* Debug LED D2 */
    #define LED_D17		(*x2cModel.outports.bLED17)     /* Debug LED D17 */

	/*------------------------------------------------------------------------*/
	/* X2C Inports */
	/*------------------------------------------------------------------------*/
	#define S2			(x2cModel.inports.bS2)		  /* Push button S2 */
	#define S3			(x2cModel.inports.bS3)		  /* Push button S3 */
	#define HA			(x2cModel.inports.bHA)		  /* Digital hall input A */
	#define HB			(x2cModel.inports.bHB)		  /* Digital hall input B */
	#define HC			(x2cModel.inports.bHC)		  /* Digital hall input C */
    #define HOME        (x2cModel.inports.bHOME)      /* Digital home input */
    #define VBUS        (x2cModel.inports.bVBUS)      /* Power supply voltage */
    #define POT         (x2cModel.inports.bPOT)       /* Potentiometer voltage */
    #define I1          (x2cModel.inports.bI1)        /* Motor current 1 */
    #define I2          (x2cModel.inports.bI2)        /* Motor current 2 */
    #define ISUM        (x2cModel.inports.bISum)      /* Sum of motor currents */

    /*------------------------------------------------------------------------*/
    /* Sample time and PWM frequency */
    /*------------------------------------------------------------------------*/
    /*
     * Sample times @ timer clock = 50 MHz.
     */
    #define SAMPLETIME_200US    (10000-1)
    #define SAMPLETIME_100US    (5000-1)
    #define SAMPLETIME_50US     (2500-1)

    /*
     * PWM frequencies @ PWM clock = 50 MHz
     */
    #define PWM_10KHZ           (5000-1)
    #define PWM_20KHZ           (2500-1)
    #define DEADTIME            (50)    /* 1us dead-time */

    /* TODO: PLEASE SELECT YOUR DESIRED SAMPLE TIME! */
    /** Sample time */
    #define X2C_SAMPLETIME      SAMPLETIME_100US


    /* TODO: PLEASE SELECT YOUR DESIRED PWM FREQUENCY! */
    /** PWM frequency */
    #define PWM_FREQUENCY       PWM_20KHZ                                   /* fPWM = 20kHz */
    #define PWM_FREQUENCY_MAX   ((uint16)((float)PWM_20KHZ*(float)0.975))   /* reduced duty cycle due to boot strapping */


    /*------------------------------------------------------------------------*/
    /* Port Pin Definitions */
    /*------------------------------------------------------------------------*/
    #define SET_LED2     (LATEbits.LATE9 = 1)
    #define CLEAR_LED2   (LATEbits.LATE9 = 0)
    #define SET_LED17    (LATEbits.LATE8 = 1)
    #define CLEAR_LED17  (LATEbits.LATE8 = 0)
        
    #define READ_S2     (!(bool)PORTDbits.RD5)
    #define READ_S3     (!(bool)PORTEbits.RE7)
    #define READ_HA     (!(bool)PORTDbits.RD1)
    #define READ_HB     (!(bool)PORTDbits.RD2)
    #define READ_HC     (!(bool)PORTDbits.RD3)
    #define READ_HOME   (!(bool)PORTDbits.RD4)


	/*------------------------------------------------------------------------*/
	/* CPU Load Measurement Macros */
	/*------------------------------------------------------------------------*/
	#define START_CPU_MEASUREMENT	(CCP3TMRL = 0x0000U)
	#define END_CPU_MEASUREMENT		(CPU_Load = CCP3TMRL)


	/*------------------------------------------------------------------------*/
	/* Global Variables */
	/*------------------------------------------------------------------------*/
	extern uint16 CPU_Load;				/* CPU load */
	extern uint16 CPU_Scale;			/* CPU load scale factor (equals 100%) */

#ifdef __cplusplus
}
#endif

#endif
