*-----------------------------------------------------------------------------
* Data transmission serial interface RS232/RS422
*-----------------------------------------------------------------------------
*  Module Description
*   - The module provides functions to transmit and receive data via
*     RS232/RS422 interface.
*   - To activate the interface a configuration of operating mode
*     (CFG_mode_RSxxx) and interface (CFG_interface_RSxxx) in file OEM.CFG
*     is necessary.
*   - Immediately after the first PLC scan the master transmits data. The
*     slave will wait to start transmitting until significant data received.
*   - MP settings transmission protocol
*       MP 5020.2  : %10101010  ;PLC modus (EXT3)
*       MP 5020.3  : %10101010  ;PLC modus (EXT4)
*       MP 5030.2  : 1          ;PLC modus (EXT3)
*       MP 5030.3  : 1          ;PLC modus (EXT4)
*
*-----------------------------------------------------------------------------

#define /s    KL_interface_RS232                       K +0
#define /s    KL_interface_RS422                       K +1

#if CFG_interface_RSxxx = "Interface_RS232"
#define /s    KL_RSxxx_interface                       K &KL_interface_RS232
#endif

#if CFG_interface_RSxxx = "Interface_RS422"
#define /s    KL_RSxxx_interface                       K &KL_interface_RS422
#endif

#define /s    KL_parameter_from_MP5xxx_2               K +0

#define /s    ML_RSxxx_status                          M[16]
#define /c    ML_RSxxx_assigned                        M &ML_RSxxx_status + 0
#define /c    ML_RSxxx_assigned_to_PLC                 M &ML_RSxxx_status + 1
#define /c    ML_RSxxx_operational                     M &ML_RSxxx_status + 2
#define /c    ML_RSxxx_transmit_buffer_empty           M &ML_RSxxx_status + 3
#define /c    ML_RSxxx_transmit_error                  M &ML_RSxxx_status + 4
#define /c    ML_RSxxx_receive_buffer_full             M &ML_RSxxx_status + 5
#define /c    ML_RSxxx_receive_error                   M &ML_RSxxx_status + 6
#define /c    ML_RSxxx_ETX_received                    M &ML_RSxxx_status + 7
#define /c    ML_RSxxx_RxD_buffer_Mod_9113             M &ML_RSxxx_status + 8

#define /s    ML_RSxxx_initial_from_PLC                M
#define /s    ML_RSxxx_slave_transmit_ready            M
#define /s    ML_RSxxx_test_on                         M

#define /s    BL_submit_identifier                     B

#include Mac_Lib.inc

;----------------------------
GLOBAL Data_transfer_RSxxx
;----------------------------

LBL Data_transfer_RSxxx

;For test in debug mode the marker ML_RSxxx_test_on can be set

  L      ML_RSxxx_test_on
  IFT
    LN     MG_RSxxx_data_transmit_on
    IFT
      S      MG_RSxxx_data_transmit_on

      INC    DG_RSxxx_transmit_dat_08
    ENDI

    LN     MG_RSxxx_data_receive_on
    S      MG_RSxxx_data_receive_on
  ENDI

;Read status

  PS     KL_RSxxx_interface
  CM     9102                          ;Read status
  PLW
  W=     ML_RSxxx_status
  M_display_module_error

  L      ML_RSxxx_transmit_error
  S      PN_error_RSxxx_transmit

  L      ML_RSxxx_receive_error
  S      PN_error_RSxxx_receive

  LN     ML_RSxxx_operational
  A      ML_RSxxx_assigned_to_PLC
  S      PN_error_RSxxx_operational

  LN     MG_RSxxx_data_transmit_on
  AN     MG_RSxxx_data_receive_on
  AN     ML_RSxxx_assigned_to_PLC
  EMT

  L      ML_RSxxx_transmit_error
  O      ML_RSxxx_receive_error
  ON     ML_RSxxx_operational
  A      ML_RSxxx_assigned_to_PLC
  R      ML_RSxxx_slave_transmit_ready
  S      PN_error_RSxxx_data_transfer_PLC
  S      ML_RSxxx_initial_from_PLC

  RPLY   BL_submit_identifier
  ==     K+0
  IFT
    SUBM   Submit_data_transfer
    =      BL_submit_identifier
    ==     K+0
    S      PN_error_submit_spawn_queue_full
  ENDI

EM

;----------------------------
LBL Submit_data_transfer
;----------------------------

;Interface enable

  LN     MG_RSxxx_data_transmit_on
  AN     MG_RSxxx_data_receive_on
  O      ML_RSxxx_initial_from_PLC
  A      ML_RSxxx_assigned_to_PLC
  IFT
    R      ML_RSxxx_initial_from_PLC

    PS     KL_RSxxx_interface          ;Interface enable
    CM     9101
    M_display_module_error

EM
  ENDI

;Interface assign to PLC

  LN     ML_RSxxx_assigned_to_PLC
  IFT
    PS     KL_RSxxx_interface
    PS     KL_parameter_from_MP5xxx_2
    CM     9100                        ;Interface assign
    M_display_module_error

EM
  ENDI

;Transmit data

  L      ML_RSxxx_transmit_buffer_empty
  A      ML_RSxxx_assigned_to_PLC
  A      ML_RSxxx_operational
  AN     ML_RSxxx_transmit_error
  A      MG_RSxxx_data_transmit_on
#if CFG_mode_RSxxx = "Slave_RSxxx"
  A      ML_RSxxx_slave_transmit_ready
#endif
  IFT
    PS     KL_RSxxx_interface
    PS     K &DG_RSxxx_transmit_dat_begin
    PS     K &DG_RSxxx_transmit_dat_end - &DG_RSxxx_transmit_dat_begin + 4
    CM     9105                        ;Transmit binary data
    M_display_module_error

    L      MG_marker_one
    R      MG_RSxxx_data_transmit_on
  ENDI

;Receive data

  L      ML_RSxxx_receive_buffer_full
  A      ML_RSxxx_assigned_to_PLC
  A      ML_RSxxx_operational
  AN     ML_RSxxx_receive_error
  IFT
    PS     KL_RSxxx_interface
    PS     K &DG_RSxxx_receive_dat_begin
    CM     9106                        ;Receive binary data
    PLW
    >      K &DG_RSxxx_receive_dat_end - &DG_RSxxx_receive_dat_begin + 4
    S      PN_error_RSxxx_buffer_overflow
    M_display_module_error

    LN     NP_M4203_error_module_9xxx
    R      PN_error_RSxxx_data_transfer_PLC
    R      PN_error_RSxxx_operational
#if CFG_mode_RSxxx = "Slave_RSxxx"
    S      ML_RSxxx_slave_transmit_ready
#endif

    L      MG_marker_one
    R      MG_RSxxx_data_receive_on
  ENDI

EM
