/*
 * File: Master.c
 *
 * Real-Time Workshop code generated for Simulink model Master.
 *
 * Model version                        : 1.257
 * Real-Time Workshop file version      : 7.3  (R2009a)  15-Jan-2009
 * Real-Time Workshop file generated on : Tue May 21 16:11:30 2013
 * TLC version                          : 7.3 (Jan 18 2009)
 * C source code generated on           : Tue May 21 16:11:31 2013
 *--------------------------------------------------------------
 *   Embedded Coder for Microchip dsPIC family.                 |
 *   Generate .c and .h files from your Matlab/simulink model   |
 *   and compile the diagram to .hex and .coff file that can be |
 *   downloaded directly into the microcontroller               |
 *                                                              |
 * Licence Accorded to Ing. Robert Grepl, Ph.D.                 |
 *                     Technical University Brno                |
 *                                                              |
 *   Written by Lubin KERHUEL -  http://www.kerhuel.eu          |
 *  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
 *   Version 3.5                               27-Apr-2011      |
 *   For Matlab 7.8             R2009a                          |
 *--------------------------------------------------------------
 */

#include "Master.h"
#include "Master_private.h"

/* Named constants for Stateflow: '<Root>/Chart' */
#define Master_IN_off                  (1)
#define Master_IN_on                   (2)
#define Master_IN_wait                 (3)
#define Master_IN_wait1                (4)

/* Named constants for Stateflow: '<Root>/RS-485' */
#define Master_IN_Off                  (1)
#define Master_IN_On                   (2)

/* Block signals (auto storage) */
BlockIO_Master Master_B;

/* Block states (auto storage) */
D_Work_Master Master_DWork;

/* Model step function */
void Master_step(void)
{
  {
    boolean_T rtb_RelationalOperator;
    uint8_T rtb_flag;
    int16_T rtb_R;
    int16_T rtb_A;
    real_T tmp;

    /* S-Function "UART_RX" Block: <Root>/Rx Input4 */
    {
      if (U2STAbits.URXDA != 0)
        _U2RXIF = 1;
      if (RxPtrHaut_Uart2 != RxPtrBas_Uart2) {
        Master_B.RxInput4 = (uint8_T) RxTab_Uart2[RxPtrBas_Uart2 ++];/* Use only the 8 low bytes or RxReg */
        RxPtrBas_Uart2 &= (Rx_BUFF_SIZE_Uart2-1);
      } else {
        Master_B.RxInput4 = 0;
      }
    }

    /* Embedded MATLAB: '<Root>/Embedded MATLAB Function1' */
    /* Embedded MATLAB Function 'Embedded MATLAB Function1': '<S2>:1' */
    if (Master_B.RxInput4 == 90) {
      /* '<S2>:1:4' */
      /* '<S2>:1:5' */
      rtb_R = 1;
    } else {
      /* '<S2>:1:7' */
      rtb_R = 0;
    }

    if (Master_B.RxInput4 == 65) {
      /* '<S2>:1:10' */
      /* '<S2>:1:11' */
      rtb_A = 1;
    } else {
      /* '<S2>:1:13' */
      rtb_A = 0;
    }

    /* Stateflow: '<Root>/RS-485' */
    /* Gateway: RS-485 */
    /* During: RS-485 */
    if (Master_DWork.temporalCounter_i1 < 3) {
      Master_DWork.temporalCounter_i1 = (uint8_T)
        (Master_DWork.temporalCounter_i1 + 1);
    }

    if (Master_DWork.is_active_c2_Master == 0) {
      /* Entry: RS-485 */
      Master_DWork.is_active_c2_Master = 1U;

      /* Transition: '<S5>:11' */
      /* Entry 'Off': '<S5>:2' */
      Master_DWork.is_c2_Master = (uint8_T)Master_IN_Off;
      Master_B.D_g = false;
    } else {
      switch (Master_DWork.is_c2_Master) {
       case Master_IN_Off:
        /* During 'Off': '<S5>:2' */
        if (rtb_A == 1) {
          /* Transition: '<S5>:6' */
          /* Exit 'Off': '<S5>:2' */
          /* Entry 'On': '<S5>:1' */
          Master_DWork.is_c2_Master = (uint8_T)Master_IN_On;
          Master_DWork.temporalCounter_i1 = 0U;
          Master_B.D_g = true;
        }
        break;

       case Master_IN_On:
        /* During 'On': '<S5>:1' */
        if (Master_DWork.temporalCounter_i1 >= 3) {
          /* Transition: '<S5>:5' */
          /* Exit 'On': '<S5>:1' */
          /* Entry 'Off': '<S5>:2' */
          Master_DWork.is_c2_Master = (uint8_T)Master_IN_Off;
          Master_B.D_g = false;
        }
        break;

       default:
        /* Transition: '<S5>:11' */
        /* Entry 'Off': '<S5>:2' */
        Master_DWork.is_c2_Master = (uint8_T)Master_IN_Off;
        Master_B.D_g = false;
        break;
      }
    }

    /* S-Function "dsPIC_Digital_OutputWrite" Block: <Root>/Digital Output Write1 */
    LATBbits.LATB11 = Master_B.D_g;

    /* Stateflow: '<Root>/Relatko' */
    /* Gateway: Relatko */
    /* During: Relatko */
    if (Master_DWork.is_active_c1_Master == 0) {
      /* Entry: Relatko */
      Master_DWork.is_active_c1_Master = 1U;

      /* Transition: '<S6>:10' */
      /* Entry 'Off': '<S6>:2' */
      Master_DWork.is_c1_Master = (uint8_T)Master_IN_Off;
      Master_B.D = false;
    } else {
      switch (Master_DWork.is_c1_Master) {
       case Master_IN_Off:
        /* During 'Off': '<S6>:2' */
        if (rtb_R == 1) {
          /* Transition: '<S6>:6' */
          /* Exit 'Off': '<S6>:2' */
          /* Entry 'On': '<S6>:1' */
          Master_DWork.is_c1_Master = (uint8_T)Master_IN_On;
          Master_B.D = true;
        }
        break;

       case Master_IN_On:
        /* During 'On': '<S6>:1' */
        if (rtb_R == 1) {
          /* Transition: '<S6>:5' */
          /* Exit 'On': '<S6>:1' */
          /* Entry 'Off': '<S6>:2' */
          Master_DWork.is_c1_Master = (uint8_T)Master_IN_Off;
          Master_B.D = false;
        }
        break;

       default:
        /* Transition: '<S6>:10' */
        /* Entry 'Off': '<S6>:2' */
        Master_DWork.is_c1_Master = (uint8_T)Master_IN_Off;
        Master_B.D = false;
        break;
      }
    }

    /* S-Function "dsPIC_Digital_OutputWrite" Block: <Root>/Digital Output Write2 */
    LATAbits.LATA2 = Master_B.D;

    /* Embedded MATLAB: '<Root>/Embedded MATLAB Function2' */
    /* Embedded MATLAB Function 'Embedded MATLAB Function2': '<S3>:1' */
    if (Master_B.RxInput4 == 49) {
      /* '<S3>:1:4' */
      /* '<S3>:1:5' */
      rtb_R = 1;
    } else if (Master_B.RxInput4 == 50) {
      /* '<S3>:1:6' */
      /* '<S3>:1:7' */
      rtb_R = 2;
    } else {
      /* '<S3>:1:9' */
      rtb_R = 0;
    }

    /* DataTypeConversion: '<Root>/Data Type Conversion3' */
    tmp = fmod((real_T)rtb_R, 256.0);
    Master_B.DataTypeConversion3 = tmp < 0.0 ? (uint8_T)(-((uint8_T)(-tmp))) :
      (uint8_T)tmp;

    /* S-Function "UART_Tx" Block: <Root>/Tx Output1 */
    {
      uint8_T Tmp;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] = Master_B.DataTypeConversion3;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
    }

    /* S-Function "UART_RX" Block: <Root>/Rx Input */
    {
      if (U1STAbits.URXDA != 0)
        _U1RXIF = 1;
      if (RxPtrHaut_Uart1 != RxPtrBas_Uart1) {
        Master_B.RxInput = (uint8_T) RxTab_Uart1[RxPtrBas_Uart1 ++];/* Use only the 8 low bytes or RxReg */
        RxPtrBas_Uart1 &= (Rx_BUFF_SIZE_Uart1-1);
      } else {
        Master_B.RxInput = 0;
      }
    }

    /* RelationalOperator: '<Root>/Relational Operator' incorporates:
     *  Constant: '<Root>/Constant'
     */
    rtb_RelationalOperator = (Master_P.Constant_Value == Master_B.RxInput);

    /* Stateflow: '<Root>/Chart' */
    /* Gateway: Chart */
    /* During: Chart */
    if (Master_DWork.temporalCounter_i1_b < 31) {
      Master_DWork.temporalCounter_i1_b = (uint8_T)
        (Master_DWork.temporalCounter_i1_b + 1);
    }

    if (Master_DWork.is_active_c3_Master == 0) {
      /* Entry: Chart */
      Master_DWork.is_active_c3_Master = 1U;

      /* Transition: '<S1>:9' */
      /* Entry 'on': '<S1>:2' */
      Master_DWork.is_c3_Master = (uint8_T)Master_IN_on;
      Master_B.D_l = false;
    } else {
      switch (Master_DWork.is_c3_Master) {
       case Master_IN_off:
        /* During 'off': '<S1>:1' */
        if (rtb_RelationalOperator == 1) {
          /* Transition: '<S1>:18' */
          /* Exit 'off': '<S1>:1' */
          /* Entry 'wait': '<S1>:17' */
          Master_DWork.is_c3_Master = (uint8_T)Master_IN_wait;
          Master_DWork.temporalCounter_i1_b = 0U;
          Master_B.D_l = false;
        }
        break;

       case Master_IN_on:
        /* During 'on': '<S1>:2' */
        if (rtb_A == 1) {
          /* Transition: '<S1>:21' */
          /* Exit 'on': '<S1>:2' */
          /* Entry 'wait1': '<S1>:19' */
          Master_DWork.is_c3_Master = (uint8_T)Master_IN_wait1;
          Master_B.D_l = false;
        }
        break;

       case Master_IN_wait:
        /* During 'wait': '<S1>:17' */
        if (Master_DWork.temporalCounter_i1_b >= 20) {
          /* Transition: '<S1>:16' */
          /* Exit 'wait': '<S1>:17' */
          /* Entry 'on': '<S1>:2' */
          Master_DWork.is_c3_Master = (uint8_T)Master_IN_on;
          Master_B.D_l = false;
        }
        break;

       case Master_IN_wait1:
        /* During 'wait1': '<S1>:19' */
        if (rtb_RelationalOperator == 1) {
          /* Transition: '<S1>:23' */
          /* Exit 'wait1': '<S1>:19' */
          /* Entry 'off': '<S1>:1' */
          Master_DWork.is_c3_Master = (uint8_T)Master_IN_off;
          Master_B.D_l = true;
        }
        break;

       default:
        /* Transition: '<S1>:9' */
        /* Entry 'on': '<S1>:2' */
        Master_DWork.is_c3_Master = (uint8_T)Master_IN_on;
        Master_B.D_l = false;
        break;
      }
    }

    /* Embedded MATLAB: '<Root>/Embedded MATLAB Function3' incorporates:
     *  Constant: '<Root>/Constant1'
     *  RelationalOperator: '<Root>/Relational Operator1'
     */
    /* Embedded MATLAB Function 'Embedded MATLAB Function3': '<S4>:1' */
    if ((Master_B.D_l == 1) && ((Master_P.Constant1_Value < Master_B.RxInput) ==
         1)) {
      /* '<S4>:1:4' */
      /* '<S4>:1:5' */
      Master_B.Tx = Master_B.RxInput;

      /* '<S4>:1:6' */
      rtb_flag = 1U;
    } else {
      /* '<S4>:1:8' */
      rtb_flag = 0U;

      /* '<S4>:1:9' */
      Master_B.Tx = 0U;
    }

    /* Outputs for enable SubSystem: '<Root>/Tx USB' incorporates:
     *  EnablePort: '<S8>/Enable'
     */
    if (rtb_flag > 0) {
      /* S-Function "UART_Tx" Block: <S8>/Tx Output */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart2 - TxPtrBas_Uart2);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
          Tmp = Tmp & (64-1);
        } while (Tmp == 0);

        TxTab_Uart2[TxPtrHaut_Uart2] = Master_B.Tx;
        TxPtrHaut_Uart2 = (TxPtrHaut_Uart2 + 1) & (64-1);
        _U2TXIF = 1;
        _U2TXIE = 1;
      }
    }

    /* end of Outputs for SubSystem: '<Root>/Tx USB' */
  }
}

/* Model initialize function */
void Master_initialize(boolean_T firstTime)
{
  (void)firstTime;

  /* Registration code */

  /* block I/O */
  (void) memset(((void *) &Master_B),0,
                sizeof(BlockIO_Master));

  /* states (dwork) */
  (void) memset((void *)&Master_DWork, 0,
                sizeof(D_Work_Master));

  /* S-Function "dsPIC_MASTER" initialization Block: <Root>/Master */
  /* Solver mode : SingleTasking */
  /* CONFIG TIMER 1 for scheduling steps 	*/
  ConfigIntTimer1(T1_INT_PRIOR_0 & T1_INT_OFF);
  T1CON = 0x8010 ;                     /* T1_PS_1_8	*/
  PR1 = 18424;

  /* Configuration TRIS */
  TRISA = 65531;
  TRISB = 38903;

  /* Configuration ADCHS */
  AD1PCFGL = (48U);

  /* S-Function "dsPIC_UART_config" initialization Block: <Root>/UART Configuration */
  /* set priority */
  _U2RXIP = 0x0007 & 3;
  _U2TXIP = 0x0007 & 1;

  /* enable/disable interrupt */
  _U2RXIE = (1 != 0);
  _U2TXIE = 0;
  U2MODE = 0x8080 + 0;
  U2STA = 0x0 + 1 + 1024 + 64 + (1<<12);
  U2BRG = 95;
  TxPtrHaut_Uart2 = 0;
  TxPtrBas_Uart2 = 0;
  RxPtrHaut_Uart2 = 0;
  RxPtrBas_Uart2 = 0;
  _U2TXIF = 0;

  /* S-Function "dsPIC_UART_config" initialization Block: <Root>/UART Configuration1 */
  /* set priority */
  _U1RXIP = 0x0007 & 4;
  _U1TXIP = 0x0007 & 2;

  /* enable/disable interrupt */
  _U1RXIE = (3 != 0);
  _U1TXIE = 0;
  U1MODE = 0x8080 + 0;
  U1STA = 0x0 + 1 + 1024 + 192 + (1<<12);
  U1BRG = 95;
  TxPtrHaut_Uart1 = 0;
  TxPtrBas_Uart1 = 0;
  RxPtrHaut_Uart1 = 0;
  RxPtrBas_Uart1 = 0;
  _U1TXIF = 0;

  /* S-Function "dsPIC_PIN_Mapping : Config" initialization Block: <Root>/PIN Mapping */
  /*unlock */
  OSCCON = 0x46;
  OSCCON = 0x57;
  OSCCONbits.IOLOCK = 0;
  RPINR18 = 65282;
  RPINR19 = 3087;
  RPOR1 = 1023;
  RPOR6 = 1791;
  RPOR7 = 65285;

  /*lock*/
  OSCCON = 0x46;
  OSCCON = 0x57;
  OSCCONbits.IOLOCK = 1;

  /* InitializeConditions for Stateflow: '<Root>/RS-485' */
  Master_DWork.is_active_c2_Master = 0U;
  Master_DWork.is_c2_Master = 0U;
  Master_B.D_g = false;

  /* InitializeConditions for Stateflow: '<Root>/Relatko' */
  Master_DWork.is_active_c1_Master = 0U;
  Master_DWork.is_c1_Master = 0U;
  Master_B.D = false;

  /* InitializeConditions for Stateflow: '<Root>/Chart' */
  Master_DWork.is_active_c3_Master = 0U;
  Master_DWork.is_c3_Master = 0U;
  Master_B.D_l = false;
}

/* File trailer for Real-Time Workshop generated code.
 *
 * [EOF]
 */
