/*
 * File: s32k312_uart_async_s32ct.c
 *
 * Code generated for Simulink model 's32k312_uart_async_s32ct'.
 *
 * Model version                   : 8.39
 * Simulink Coder version          : 23.2 (R2023b) 01-Aug-2023
 * MBDT for S32K3 Series Version  :  Version 1.6.0 (R2021a-R2024b) 27-Jan-2025
 * C/C++ source code generated on  : Sat Sep 27 20:43:36 2025
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: ARM Compatible->ARM Cortex
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "s32k312_uart_async_s32ct.h"
#include "s32k312_uart_async_s32ct_private.h"
#include "rtwtypes.h"
#include <string.h>
#include "StandardTypes.h"
#include "zero_crossing_types.h"

/* Exported data definition */

/* Volatile memory section */
/* Definition for custom storage class: Volatile */
volatile uint32_T remainingBytes;      /* '<Root>/Data Store Memory5' */
volatile uint32_T status;              /* '<Root>/Data Store Memory6' */
volatile uint32_T transfer_flag;       /* '<Root>/Data Store Memory8' */
volatile Std_VersionInfoType versionInfo;/* '<Root>/Data Store Memory7' */

/* Block signals (default storage) */
B_s32k312_uart_async_s32ct_T s32k312_uart_async_s32ct_B;

/* Previous zero-crossings (trigger) states */
PrevZCX_s32k312_uart_async_s3_T s32k312_uart_async_s32c_PrevZCX;

/* Real-time model */
static RT_MODEL_s32k312_uart_async_s_T s32k312_uart_async_s32ct_M_;
RT_MODEL_s32k312_uart_async_s_T *const s32k312_uart_async_s32ct_M =
  &s32k312_uart_async_s32ct_M_;
void MBDT_Uart_Callback (uint8 Channel, Uart_EventType Event)
{
  s32k312_uart_async_s32ct_B.Hardware_Interrupt_Handler2_o2 = Channel;
  s32k312_uart_async_s32ct_B.Hardware_Interrupt_Handler2_o3 = Event;

  /* Output and update for function-call system: '<Root>/UartCallback' */

  /* If: '<S4>/If' */
  if (s32k312_uart_async_s32ct_B.Hardware_Interrupt_Handler2_o3 == 2U) {
    /* Outputs for IfAction SubSystem: '<S4>/If Action Subsystem' incorporates:
     *  ActionPort: '<S5>/Action Port'
     */
    /* S-Function (s32k3_uart): '<S5>/Uart_GetStatus' */
    s32k312_uart_async_s32ct_B.Uart_GetStatus_o2 = Uart_GetStatus(0,
      &s32k312_uart_async_s32ct_B.Uart_GetStatus_o1, UART_RECEIVE);

    /* DataStoreWrite: '<S5>/Data Store Write1' */
    remainingBytes = s32k312_uart_async_s32ct_B.Uart_GetStatus_o1;

    /* DataStoreWrite: '<S5>/Data Store Write3' */
    status = s32k312_uart_async_s32ct_B.Uart_GetStatus_o2;

    /* SwitchCase: '<S5>/Switch Case' incorporates:
     *  DataStoreRead: '<S5>/Data Store Read'
     */
    switch ((int32_T)transfer_flag) {
     case 1:
      /* Outputs for IfAction SubSystem: '<S5>/Send1' incorporates:
       *  ActionPort: '<S9>/Action Port'
       */
      /* DataStoreWrite: '<S9>/Data Store Write1' incorporates:
       *  Constant: '<S9>/Constant3'
       */
      transfer_flag = s32k312_uart_async_s32ct_P.Constant3_Value_f;

      /* S-Function (s32k3_uart): '<S9>/Uart_AsyncSend' */
      s32k312_uart_async_s32ct_B.Uart_AsyncSend_j = Uart_AsyncSend(0,
        &s32k312_uart_async_s32ct_B.Uart_AsyncReceive_o1, 1);

      /* End of Outputs for SubSystem: '<S5>/Send1' */
      break;

     case 2:
      /* Outputs for IfAction SubSystem: '<S5>/Receive' incorporates:
       *  ActionPort: '<S6>/Action Port'
       */
      /* DataStoreWrite: '<S6>/Data Store Write' incorporates:
       *  Constant: '<S6>/Constant1'
       */
      transfer_flag = s32k312_uart_async_s32ct_P.Constant1_Value;

      /* S-Function (s32k3_uart): '<S6>/Uart_AsyncReceive' incorporates:
       *  Constant: '<S6>/Constant'
       */
      s32k312_uart_async_s32ct_B.Uart_AsyncReceive_o2_m = Uart_AsyncReceive(0,
        &s32k312_uart_async_s32ct_B.Uart_AsyncReceive_o1_p,
        s32k312_uart_async_s32ct_P.Constant_Value);

      /* End of Outputs for SubSystem: '<S5>/Receive' */
      break;

     case 3:
      /* Outputs for IfAction SubSystem: '<S5>/Send' incorporates:
       *  ActionPort: '<S8>/Action Port'
       */
      /* DataStoreWrite: '<S8>/Data Store Write1' incorporates:
       *  Constant: '<S8>/Constant3'
       */
      transfer_flag = s32k312_uart_async_s32ct_P.Constant3_Value_e;

      /* S-Function (s32k3_uart): '<S8>/Uart_AsyncSend' */
      s32k312_uart_async_s32ct_B.Uart_AsyncSend = Uart_AsyncSend(1,
        &s32k312_uart_async_s32ct_B.Uart_AsyncReceive_o1_p, 1);

      /* End of Outputs for SubSystem: '<S5>/Send' */
      break;

     case 4:
      /* Outputs for IfAction SubSystem: '<S5>/Receive1' incorporates:
       *  ActionPort: '<S7>/Action Port'
       */
      /* DataStoreWrite: '<S7>/Data Store Write' incorporates:
       *  Constant: '<S7>/Constant1'
       */
      transfer_flag = s32k312_uart_async_s32ct_P.Constant1_Value_m;

      /* S-Function (s32k3_uart): '<S7>/Uart_AsyncReceive' incorporates:
       *  Constant: '<S7>/Constant'
       */
      s32k312_uart_async_s32ct_B.Uart_AsyncReceive_o2 = Uart_AsyncReceive(1,
        &s32k312_uart_async_s32ct_B.Uart_AsyncReceive_o1,
        s32k312_uart_async_s32ct_P.Constant_Value_p);

      /* End of Outputs for SubSystem: '<S5>/Receive1' */
      break;
    }

    /* End of SwitchCase: '<S5>/Switch Case' */
    /* End of Outputs for SubSystem: '<S4>/If Action Subsystem' */
  }

  /* End of If: '<S4>/If' */
}

/* Model step function */
void s32k312_uart_async_s32ct_step(void)
{
  int32_T i;

  /* S-Function (s32k3_dio): '<Root>/DIO_ReadChannel_SW0' */
  s32k312_uart_async_s32ct_B.DIO_ReadChannel_SW0 = Dio_ReadChannel
    (DioConf_DioChannel_USER_SW0);

  /* Outputs for Triggered SubSystem: '<Root>/DMA Pause' incorporates:
   *  TriggerPort: '<S1>/Trigger'
   */
  if ((s32k312_uart_async_s32ct_B.DIO_ReadChannel_SW0 > 0) &&
      (s32k312_uart_async_s32c_PrevZCX.DMAPause_Trig_ZCE != POS_ZCSIG)) {
    /* S-Function (s32k3_mcl): '<S1>/Mcl8' */
    Mcl_SetDmaInstanceCommand(DMA_LOGIC_INST_0, MCL_DMA_INST_PAUSE);

    /* S-Function (s32k3_dio): '<S1>/ ' incorporates:
     *  Constant: '<S1>/Constant2'
     */
    Dio_WriteChannel(DioConf_DioChannel_RGBLED0_RED,
                     s32k312_uart_async_s32ct_P.Constant2_Value_o);

    /* S-Function (s32k3_dio): '<S1>/   ' incorporates:
     *  Constant: '<S1>/Constant'
     */
    Dio_WriteChannel(DioConf_DioChannel_RGBLED0_GREEN,
                     s32k312_uart_async_s32ct_P.Constant_Value_g);
  }

  s32k312_uart_async_s32c_PrevZCX.DMAPause_Trig_ZCE = (ZCSigState)
    (s32k312_uart_async_s32ct_B.DIO_ReadChannel_SW0 > 0);

  /* End of Outputs for SubSystem: '<Root>/DMA Pause' */

  /* S-Function (s32k3_dio): '<Root>/DIO_ReadChannel_SW1' */
  s32k312_uart_async_s32ct_B.DIO_ReadChannel_SW1 = Dio_ReadChannel
    (DioConf_DioChannel_USER_SW1);

  /* Outputs for Triggered SubSystem: '<Root>/DMA Resume' incorporates:
   *  TriggerPort: '<S2>/Trigger'
   */
  if ((s32k312_uart_async_s32ct_B.DIO_ReadChannel_SW1 > 0) &&
      (s32k312_uart_async_s32c_PrevZCX.DMAResume_Trig_ZCE != POS_ZCSIG)) {
    /* S-Function (s32k3_mcl): '<S2>/Mcl8' */
    Mcl_SetDmaInstanceCommand(DMA_LOGIC_INST_0, MCL_DMA_INST_RESUME);

    /* DataStoreWrite: '<S2>/Data Store Write' incorporates:
     *  Constant: '<S2>/Constant3'
     */
    transfer_flag = s32k312_uart_async_s32ct_P.Constant3_Value;

    /* S-Function (s32k3_dio): '<S2>/ ' incorporates:
     *  Constant: '<S2>/Constant'
     */
    Dio_WriteChannel(DioConf_DioChannel_RGBLED0_RED,
                     s32k312_uart_async_s32ct_P.Constant_Value_gk);

    /* S-Function (s32k3_dio): '<S2>/   ' incorporates:
     *  Constant: '<S2>/Constant2'
     */
    Dio_WriteChannel(DioConf_DioChannel_RGBLED0_GREEN,
                     s32k312_uart_async_s32ct_P.Constant2_Value_d);

    /* Constant: '<S2>/Constant LF and CR ' incorporates:
     *  Concatenate: '<S2>/Vector Concatenate'
     */
    s32k312_uart_async_s32ct_B.VectorConcatenate_i[0] =
      s32k312_uart_async_s32ct_P.ConstantLFandCR_Value[0];

    /* Constant: '<S2>/Constant LF and CR  ' incorporates:
     *  Concatenate: '<S2>/Vector Concatenate'
     */
    s32k312_uart_async_s32ct_B.VectorConcatenate_i[37] =
      s32k312_uart_async_s32ct_P.ConstantLFandCR_Value_c[0];

    /* Constant: '<S2>/Constant LF and CR ' incorporates:
     *  Concatenate: '<S2>/Vector Concatenate'
     */
    s32k312_uart_async_s32ct_B.VectorConcatenate_i[1] =
      s32k312_uart_async_s32ct_P.ConstantLFandCR_Value[1];

    /* Constant: '<S2>/Constant LF and CR  ' incorporates:
     *  Concatenate: '<S2>/Vector Concatenate'
     */
    s32k312_uart_async_s32ct_B.VectorConcatenate_i[38] =
      s32k312_uart_async_s32ct_P.ConstantLFandCR_Value_c[1];

    /* Constant: '<S2>/Constant1' */
    for (i = 0; i < 35; i++) {
      /* Concatenate: '<S2>/Vector Concatenate' incorporates:
       *  Constant: '<S2>/Constant1'
       */
      s32k312_uart_async_s32ct_B.VectorConcatenate_i[i + 2] =
        s32k312_uart_async_s32ct_P.Constant1_Value_d[i];
    }

    /* End of Constant: '<S2>/Constant1' */

    /* S-Function (s32k3_uart): '<S2>/Uart_SendMessage' */
    s32k312_uart_async_s32ct_B.Uart_SendMessage_k = Uart_AsyncSend(0,
      &s32k312_uart_async_s32ct_B.VectorConcatenate_i[0], 39);
  }

  s32k312_uart_async_s32c_PrevZCX.DMAResume_Trig_ZCE = (ZCSigState)
    (s32k312_uart_async_s32ct_B.DIO_ReadChannel_SW1 > 0);

  /* End of Outputs for SubSystem: '<Root>/DMA Resume' */
}

/* Model initialize function */
void s32k312_uart_async_s32ct_initialize(void)
{
  /* Start for DataStoreMemory: '<Root>/Data Store Memory5' */
  remainingBytes = s32k312_uart_async_s32ct_P.DataStoreMemory5_InitialValue;

  /* Start for DataStoreMemory: '<Root>/Data Store Memory6' */
  status = s32k312_uart_async_s32ct_P.DataStoreMemory6_InitialValue;

  /* Start for DataStoreMemory: '<Root>/Data Store Memory8' */
  transfer_flag = s32k312_uart_async_s32ct_P.DataStoreMemory8_InitialValue;

  /* Start for DataStoreMemory: '<Root>/Data Store Memory7' */
  versionInfo = s32k312_uart_async_s32ct_P.DataStoreMemory7_InitialValue;
  s32k312_uart_async_s32c_PrevZCX.DMAPause_Trig_ZCE = POS_ZCSIG;
  s32k312_uart_async_s32c_PrevZCX.DMAResume_Trig_ZCE = POS_ZCSIG;

  /* SystemInitialize for S-Function (s32k3_isr_handler): '<Root>/Hardware_Interrupt_Handler2' incorporates:
   *  SubSystem: '<Root>/UartCallback'
   */
  /* System initialize for function-call system: '<Root>/UartCallback' */

  /* SystemInitialize for IfAction SubSystem: '<S4>/If Action Subsystem' */
  /* SystemInitialize for IfAction SubSystem: '<S5>/Receive' */
  /* SystemInitialize for S-Function (s32k3_uart): '<S6>/Uart_AsyncReceive' incorporates:
   *  Outport: '<S6>/Input4'
   */
  s32k312_uart_async_s32ct_B.Uart_AsyncReceive_o1_p =
    s32k312_uart_async_s32ct_P.Input4_Y0;

  /* End of SystemInitialize for SubSystem: '<S5>/Receive' */

  /* SystemInitialize for IfAction SubSystem: '<S5>/Receive1' */
  /* SystemInitialize for S-Function (s32k3_uart): '<S7>/Uart_AsyncReceive' incorporates:
   *  Outport: '<S7>/Input4'
   */
  s32k312_uart_async_s32ct_B.Uart_AsyncReceive_o1 =
    s32k312_uart_async_s32ct_P.Input4_Y0_l;

  /* End of SystemInitialize for SubSystem: '<S5>/Receive1' */
  /* End of SystemInitialize for SubSystem: '<S4>/If Action Subsystem' */

  /* End of SystemInitialize for S-Function (s32k3_isr_handler): '<Root>/Hardware_Interrupt_Handler2' */

  /* Outputs for Atomic SubSystem: '<Root>/Initialize Function' */
  /* SignalConversion generated from: '<S3>/Vector Concatenate' incorporates:
   *  Concatenate: '<S3>/Vector Concatenate'
   *  Constant: '<S3>/Constant LF and CR1'
   */
  s32k312_uart_async_s32ct_B.VectorConcatenate[0] =
    s32k312_uart_async_s32ct_P.ConstantLFandCR1_Value[0];
  s32k312_uart_async_s32ct_B.VectorConcatenate[1] =
    s32k312_uart_async_s32ct_P.ConstantLFandCR1_Value[1];

  /* SignalConversion generated from: '<S3>/Vector Concatenate' incorporates:
   *  Concatenate: '<S3>/Vector Concatenate'
   *  Constant: '<S3>/Constant3'
   *  DataStoreWrite: '<S3>/Data Store Write3'
   */
  memcpy(&s32k312_uart_async_s32ct_B.VectorConcatenate[2],
         &s32k312_uart_async_s32ct_P.Constant3_Value_d[0], 105U * sizeof(uint8_T));

  /* SignalConversion generated from: '<S3>/Vector Concatenate' incorporates:
   *  Concatenate: '<S3>/Vector Concatenate'
   *  Constant: '<S3>/Constant LF and CR'
   */
  s32k312_uart_async_s32ct_B.VectorConcatenate[107] =
    s32k312_uart_async_s32ct_P.ConstantLFandCR_Value_j[0];
  s32k312_uart_async_s32ct_B.VectorConcatenate[108] =
    s32k312_uart_async_s32ct_P.ConstantLFandCR_Value_j[1];

  /* S-Function (s32k3_uart): '<S3>/Uart_SendMessage' */
  s32k312_uart_async_s32ct_B.Uart_SendMessage = Uart_AsyncSend(0,
    &s32k312_uart_async_s32ct_B.VectorConcatenate[0], 109);

  /* DataStoreWrite: '<S3>/Data Store Write' incorporates:
   *  Constant: '<S3>/Constant2'
   */
  transfer_flag = s32k312_uart_async_s32ct_P.Constant2_Value;

  /* S-Function (s32k3_uart): '<S3>/Uart_GetVersionInfo' */
  Uart_GetVersionInfo(&s32k312_uart_async_s32ct_B.Uart_GetVersionInfo);

  /* DataStoreWrite: '<S3>/Data Store Write2' */
  versionInfo = s32k312_uart_async_s32ct_B.Uart_GetVersionInfo;

  /* End of Outputs for SubSystem: '<Root>/Initialize Function' */
}

/* Model terminate function */
void s32k312_uart_async_s32ct_terminate(void)
{
  /* (no terminate code required) */
}

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