/*
 * -------------------------------------------------------------------
 * MPLAB 16-Bit Device Blocks for Simulink v3.38.
 *
 *   Product Page:  http://www.microchip.com/SimulinkBlocks
 *           Forum: http://www.microchip.com/forums/f192.aspx
 *           Wiki:  http://microchip.wikidot.com/simulink:start
 * -------------------------------------------------------------------
 * File: dsPIC_main_22_4_2018_main.c
 *
 * Code generated for Simulink model 'dsPIC_main_22_4_2018'.
 *
 * Model version                  : 1.96
 * Simulink Coder version         : 8.9 (R2015b) 13-Aug-2015
 * C/C++ source code generated on : Sun Apr 22 14:14:28 2018
 */

#define MCHP_isMainFile
#include "dsPIC_main_22_4_2018.h"

/* Microchip Global Variables */
volatile uint_T BusyFlagRecursion __attribute__ ((near)) = 0;/* Set Fuses Options */
_FOSCSEL( FNOSC_PRI ) ;
_FOSC( POSCMD_HS & OSCIOFNC_ON & FCKSM_CSECMD ) ;
_FWDT( FWDTEN_OFF ) ;
_FPOR( FPWRT_PWR64 & BOREN_OFF ) ;
_FICD( JTAGEN_OFF ) ;

/* Scheduler */
void __attribute__((__interrupt__,__auto_psv__)) _T1Interrupt(void)
{
  {
    extern volatile uint_T BusyFlagRecursion __attribute__ ((near)) ;
    struct {
      unsigned int Flags0 : 1;
      unsigned int Flags1 : 1;
    } static volatile Overrun;

    struct {
      unsigned int Flags0 : 1;
      unsigned int Flags1 : 1;
    } static volatile event;

    static int_T taskCounter[2] = { 0, 0 };

    _T1IF = 0;                         /* Re-enable interrupt */

    /* Set busy flag */
    __asm__ volatile ("INC %[MyVar]" : [MyVar] "+m" (BusyFlagRecursion) );/* ensure atomic increment of BusyFlagRecursion */
    LATBbits.LATB12 = 1;

    /* Check subrate overrun, set rates that need to run this time step*/
    if (taskCounter[1] == 0) {         /* task dropped on overload */
      event.Flags1 = 1U;
    }

    /* Update task internal counters */
    taskCounter[1]++;
    if (taskCounter[1] == 10) {
      taskCounter[1]= 0;
    }

    /* Step the model for base rate */
    /* Start profiling task 0 */
    dsPIC_main_22_4_2018_step0();

    /* Get model outputs here */
    _T1IF = 0;                         /* Re-enable interrupt */

    /* Stop profiling task 0 */
    if (_T1IF ) {
      __asm__ volatile ("DEC %[MyVar]" : [MyVar] "+m" (BusyFlagRecursion) );/* ensure atomic decrement of BusyFlagRecursion */
      return;                          /* Will re-enter into the interrupt */
    }

    /* Re-Enable Interrupt. IPL value is 2 at this point */
    _IPL0 = 1;                         /* Set IPL to 1 (interrupt priority is 2) */
    _IPL1 = 0;

    /* Step the model for any subrate */
    /* Handle Task 1 */
    if (Overrun.Flags1) {
      __asm__ volatile ("DEC %[MyVar]" : [MyVar] "+m" (BusyFlagRecursion) );/* ensure atomic decrement of BusyFlagRecursion */
      return;                          /* Priority to higher rate steps interrupted */
    }

    if (event.Flags1) {
      Overrun.Flags1 = 1;
      do {
        /* Start profiling task 1 */
        event.Flags1 = 0U;
        dsPIC_main_22_4_2018_step1();

        /* Get model outputs here */
        ;                              /* Execute task tid 1 */

        /* Stop profiling task 1 */
      } while (event.Flags1);

      Overrun.Flags1 = 0U;
    }

    /* Disable Interrupt. IPL value is 1 at this point */
    _IPL1 = 1;                         /* Set IPL to 2 (interrupt priority was 1) */
    _IPL0 = 0;

    /* Release busy flag */
    __asm__ volatile ("DEC %[MyVar]" : [MyVar] "+m" (BusyFlagRecursion) );/* ensure atomic decrement of BusyFlagRecursion */
    if (BusyFlagRecursion == 0)
      LATBbits.LATB12 = 0;
  }
}

int main()
{
  /* Initialize model */
  /* Start Clock Switching */
  if ((OSCCONbits.COSC & 1) && !(OSCCONbits.COSC & 4))/* check not already in PLL mode (0?1) */
  {
    __builtin_write_OSCCONH(0);        /* Clock Switch to non PLL mode */
    __builtin_write_OSCCONL(0x01);     /* Start clock switching */
    __asm__ volatile("nop\n"
                     "nop");
    while (OSCCONbits.COSC != 0) ;
  }

  PLLFBD = 0x60;                       /* configure Oscillator PLL : PLLDIV */
  CLKDIV = 0x05;                       /* configure Oscillator PLL : FRCDIV ; PLLPOST ; PLLPRE */
  __builtin_write_OSCCONH(3);          /* Clock Switch to desired configuration */
  __builtin_write_OSCCONL(0x01);       /* Start clock switching */

  /* Configure Pins as Analog or Digital */
  ANSELB = 0xEFFF;
  ANSELC = 0x4000;
  ANSELE = 0x7F;
  ANSELG = 0x0200;

  /* Configure Remappables Pins */
  RPINR18 = 0x42;
  RPINR20 = 0x3D;
  RPOR0 = 0x0106;

  /* Configure Digitals I/O directions */
  TRISB = 0xEFFF;
  TRISD = 0xF7FC;
  TRISE = 0xFF7F;
  TRISG = 0xFEBF;

  /* Finish clock switching procedure */
  while (OSCCONbits.COSC != 3) ;       /* Wait for Oscillator Stabilisation */
  while (OSCCONbits.LOCK != 1) ;       /* Wait for PLL Stabilisation */

  /* Initialize model */
  dsPIC_main_22_4_2018_initialize();

  /* Configure Timers */
  /* --- TIMER 1 --- This timer is enabled at end of configuration functions. */
  _T1IP = 2;                           /* Set timer Interrupt Priority */
  _T1IF = 0;                           /* Reset pending Interrupt */
  _T1IE = 1;                           /* Enable Timer Interrupt. */
  PR1 = 0x02BB;                        /* Period */

  /* Enable Time-step */
  TMR1 = 0x02BA;
  T1CON = 0x8000;                      /* Timer 1 is the source trigger for the model Time-step */

  /* Main Loop */
  for (;;) ;
}                                      /* end of main() */

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
