/*
 * -------------------------------------------------------------------
 * 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.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
 */

#include "dsPIC_main_22_4_2018.h"
#include "dsPIC_main_22_4_2018_private.h"

/* Block signals and states (auto storage) */
DW_dsPIC_main_22_4_2018_T dsPIC_main_22_4_2018_DW;

/* Real-time model */
RT_MODEL_dsPIC_main_22_4_2018_T dsPIC_main_22_4_2018_M_;
RT_MODEL_dsPIC_main_22_4_2018_T *const dsPIC_main_22_4_2018_M =
  &dsPIC_main_22_4_2018_M_;

/* Model step function for TID0 */
void dsPIC_main_22_4_2018_step0(void)  /* Sample time: [1.0E-5s, 0.0s] */
{
  uint16_T tmp;

  /* DataTypeConversion: '<Root>/Data Type Conversion2' incorporates:
   *  Sin: '<Root>/Sine Wave'
   */
  tmp = (uint16_T)fmod((uint16_T)floor(sin((real_T)
    dsPIC_main_22_4_2018_DW.counter * 2.0 * 3.1415926535897931 / 28.0) * 16384.0
    + 32767.0), 65536.0);

  /* MATLAB Function: '<Root>/MATLAB Function' incorporates:
   *  DataTypeConversion: '<Root>/Data Type Conversion2'
   */
  /* MATLAB Function 'MATLAB Function': '<S1>:1' */
  /* '<S1>:1:2' u = uint16(u); */
  /* '<S1>:1:3' y = typecast(uint16(u), 'uint8'); */
  memcpy(&dsPIC_main_22_4_2018_DW.y[0], &tmp, (size_t)2 * sizeof(uint8_T));

  /* S-Function (MCHP_BUS_SPI): '<Root>/SPI2_external_dac_com' */
  /* number of SPI blocks : 1 ; Current: 1 ; MCHP_SPI_StartImplemented =  1*/
  if (MCHP_SPI2_State == 0)            /* Free for next sequence ?*/
  {
    SPI2_Buff8[0]= ((uint8_T)0U);
    SPI2_Buff8[1]= dsPIC_main_22_4_2018_DW.y[1];
    SPI2_Buff8[2]= dsPIC_main_22_4_2018_DW.y[0];
    MCHP_SPI2_State = 1;
    _SPI2IF = 1;                       /* Force Interrupt */
  }

  /* Update for Sin: '<Root>/Sine Wave' */
  dsPIC_main_22_4_2018_DW.counter++;
  if (dsPIC_main_22_4_2018_DW.counter == 28L) {
    dsPIC_main_22_4_2018_DW.counter = 0L;
  }

  /* End of Update for Sin: '<Root>/Sine Wave' */
}

/* Model step function for TID1 */
void dsPIC_main_22_4_2018_step1(void)  /* Sample time: [0.0001s, 0.0s] */
{
  /* S-Function (MCHP_BUS_SPI): '<Root>/SPI1_external_adc_readings' */
  /* number of SPI blocks : 1 ; Current: 1 ; MCHP_SPI_StartImplemented =  1*/
  if (MCHP_SPI1_State == 0)            /* Free for next sequence ?*/
  {
    dsPIC_main_22_4_2018_DW.U1CH1 = SPI1_Buff8[0];
    dsPIC_main_22_4_2018_DW.SPI1_external_adc_readings_o2 = SPI1_Buff8[1];
    MCHP_SPI1_State = 1;
    _SPI1IF = 1;                       /* Force Interrupt */
  }

  /* MATLAB Function: '<Root>/Message packeting' */
  /* MATLAB Function 'Message packeting': '<S3>:1' */
  /* '<S3>:1:2' message = uint8(zeros(1,6)); */
  /* alokace pameti pro zasilanou zpravu */
  /* '<S3>:1:3' message(1,1:6) = uint8([10, 20, 30, MSB, LSB, 36]); */
  dsPIC_main_22_4_2018_DW.message[0] = 10U;
  dsPIC_main_22_4_2018_DW.message[1] = 20U;
  dsPIC_main_22_4_2018_DW.message[2] = 30U;
  dsPIC_main_22_4_2018_DW.message[3] = dsPIC_main_22_4_2018_DW.U1CH1;
  dsPIC_main_22_4_2018_DW.message[4] =
    dsPIC_main_22_4_2018_DW.SPI1_external_adc_readings_o2;
  dsPIC_main_22_4_2018_DW.message[5] = 36U;

  /* postaveni zpravy k zapsani */
  /* start 10,20,30 */
  /* Data_H */
  /* Data_L */
  /* stop 36 */

  /* MCHP_UART_Tx Block for UARTRef 1: <Root>/UART Tx2/Outputs */
  {
    uint_T Tmp;
    Tmp = ~(MCHP_UART1_Tx.tail - MCHP_UART1_Tx.head);
    Tmp = Tmp & (Tx_BUFF_SIZE_Uart1 - 1);/* Modulo Buffer Size */

    {
      int_T i1;
      uint8_T *u0 = &dsPIC_main_22_4_2018_DW.message[0] ;
      for (i1 = 0; i1 < 6 ; i1++) {
        if (Tmp != 0) {
          MCHP_UART1_Tx.buffer[MCHP_UART1_Tx.tail] = u0[i1];
          MCHP_UART1_Tx.tail = (MCHP_UART1_Tx.tail + 1) & (Tx_BUFF_SIZE_Uart1 -
            1);
          Tmp--;
        } else
          break;
      }
    }

    if (1U == U1STAbits.TRMT)
      _U1TXIF = 1;                     /* Tx Buffer is empty, enable Interrupt */
  }
}

/* Model initialize function */
void dsPIC_main_22_4_2018_initialize(void)
{
  /* Registration code */

  /* states (dwork) */
  (void) memset((void *)&dsPIC_main_22_4_2018_DW, 0,
                sizeof(DW_dsPIC_main_22_4_2018_T));

  /* S-Function "Microchip MASTER" initialization Block: <Root>/Microchip Master */

  /* Start for S-Function (MCHP_BUS_SPI): '<Root>/SPI2_external_dac_com' */
  /* Set-up SPI 2 peripheral with Fsck = 3500000  (3500000 with  0.0 \% error)  */
  SPI2CON1 = 0x01AE;
  SPI2CON2 = 0x01;
  SPI2STAT = 0x8014;
  _SPI2IP = 4;                         /* Set SPI Interrupt Priority */
  _SPI2IF = 0;                         /* Reset interrupt Flag */
  _SPI2IE = 1;                         /* Enable Interrupts */

  /* Start for S-Function (MCHP_BUS_SPI): '<Root>/SPI1_external_adc_readings' */
  /* Set-up SPI 1 peripheral with Fsck = 2500000  (2500000 with  0.0 \% error)  */
  SPI1CON1 = 0x09A6;
  SPI1CON2 = 0x01;
  SPI1STAT = 0x8014;
  _SPI1IP = 4;                         /* Set SPI Interrupt Priority */
  _SPI1IF = 0;                         /* Reset interrupt Flag */
  _SPI1IE = 1;                         /* Enable Interrupts */

  /* MCHP_UART_Config Block for UART 1: <Root>/UART Configuration/Initialize */
  U1BRG = 0x25;                        /* Baud rate: 460800 (-0.06%) */
  U1MODE = 0x8008;
  U1STA = 0x2400;

  /* Configure UART1 Tx Interruption */
  MCHP_UART1_Tx.head = 0;              /* Initialise Circular Buffers */
  MCHP_UART1_Tx.tail = 0;
  _U1TXIP = 1;                         /*  Tx Interrupt priority set to 1 */
  _U1TXIF = 0;                         /*  */
  _U1TXIE = 1;                         /* Enable Interrupt */

  /* Configure UART1 Rx Interruption */
  MCHP_UART1_Rx.head = 0;              /* Initialise Circular Buffers */
  MCHP_UART1_Rx.tail = 0;
  _U1RXIP = 1;                         /* Rx Interrupt priority set to 1 */
  _U1RXIF = 0;                         /*  */
  _U1RXIE = 1;                         /* Enable Interrupt */

  /* InitializeConditions for Sin: '<Root>/Sine Wave' */
  dsPIC_main_22_4_2018_DW.counter = 0L;
}

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