#define UART1_USE_AS_MODBUS

#define ATOMIC_ASSIGN(y,x) do { \
   y=x;  \
   while ((y&0xff00)!=((x)&0xff00)) y=x; \
   } while(0);

#include "mcc_generated_files/system/system.h"
#include "modbus.h"    
#ifdef UART1_USE_AS_MODBUS 
    #include "mcc_generated_files/uart/uart1.h"
    //because defined in uart1.h
    extern volatile uint16_t tick4rxd1;    //Uart1 rxd tick  (Also used for txd)
    extern volatile uint16_t tmr4rxd1;    //Uart1 rxd timer1 value  (Also used for txd)
#else
    volatile uint16_t tick4rxd1;    //Uart1 rxd tick  (Also used for txd)
    volatile uint16_t tmr4rxd1;    //Uart1 rxd timer1 value  (Also used for txd)
#endif
#ifdef UART2_USE_AS_MODBUS 
    #include "mcc_generated_files/uart/uart2.h"
    //because defined in uart2.h
    extern volatile uint16_t tick4rxd2;    //Uart2 rxd tick
    extern volatile uint16_t tmr4rxd2;    //Uart2 rxd timer1 value
#else
    volatile uint16_t tick4rxd2;    //Uart2 rxd tick
    volatile uint16_t tmr4rxd2;    //Uart2 rxd timer1 value
#endif
#ifdef UART3_USE_AS_MODBUS 
    #include "mcc_generated_files/uart3.h"
    //because defined in uart3.h
    extern volatile uint16_t tick4rxd3;    //Uart3 rxd tick
    extern volatile uint16_t tmr4rxd3;    //Uart3 rxd timer1 value
#else
    volatile uint16_t tick4rxd3;    //Uart3 rxd tick
    volatile uint16_t tmr4rxd3;    //Uart3 rxd timer1 value
#endif
#ifdef UART4_USE_AS_MODBUS 
    #include "mcc_generated_files/uart4.h"
    //because defined in uart4.h
    extern volatile uint16_t tick4rxd4;    //Uart4 rxd tick
    extern volatile uint16_t tmr4rxd4;    //Uart4 rxd timer1 value
#else
    volatile uint16_t tick4rxd4;    //Uart4 rxd tick
    volatile uint16_t tmr4rxd4;    //Uart4 rxd timer1 value    
#endif


#define MCU_UART_QTY 1

#include "modbus_User.h"
//#include "mcc_generated_files/memory/flash.h"
uint8_t my_MCU_UART_QTY=MCU_UART_QTY;


#ifndef UART1_USE_AS_MODBUS   //XC8 ignore  __attribute__ ((weak))
void __attribute__((weak)) EnableUART1_RS485Receive (unsigned char enable) // enable=1 => receive enable
{
    //default code for pass compile
    // enable = 0 => TRANSMIT DATA, 0 = "output". enable = 1 => RECEIVE DATA, 1 = "input"
}
#endif

#ifndef UART2_USE_AS_MODBUS   //XC8 ignore  __attribute__ ((weak))  
void __attribute__((weak)) EnableUART2_RS485Receive (unsigned char enable) 
{
    //default code for pass compile
    // enable = 0 => TRANSMIT DATA, 0 = "output". enable = 1 => RECEIVE DATA, 1 = "input"
}
#endif

#ifndef UART3_USE_AS_MODBUS   //XC8 ignore  __attribute__ ((weak))  
void __attribute__((weak)) EnableUART3_RS485Receive (unsigned char enable) 
{
    //default code for pass compile
    // enable = 0 => TRANSMIT DATA, 0 = "output". enable = 1 => RECEIVE DATA, 1 = "input"
}
#endif

#ifndef UART4_USE_AS_MODBUS   //XC8 ignore  __attribute__ ((weak)) 
void __attribute__((weak)) EnableUART4_RS485Receive (unsigned char enable) 
{
    //default code for pass compile
    // enable = 0 => TRANSMIT DATA, 0 = "output". enable = 1 => RECEIVE DATA, 1 = "input"
}
#endif




    
//only for decrease memory consumed , not for architecture difference    
#if MCU_UART_QTY==-1     //simulink will create MCU_UART_QTY=-1 when we don't use UART
    uint8_t LinearBufferTx1[1]; 
    LinearBuf_type LinearTx1;  
    uint8_t LinearBufferTx2[1];
    LinearBuf_type LinearTx2;    
    uint8_t LinearBufferTx3[1];
    LinearBuf_type LinearTx3;  
    uint8_t LinearBufferTx4[1];
    LinearBuf_type LinearTx4;   
#endif
#if MCU_UART_QTY==1   
    #ifdef UART1_USE_AS_MODBUS
    uint8_t LinearBufferTx1[256]; 
    #else
    uint8_t LinearBufferTx1[1]; 
    #endif
    LinearBuf_type LinearTx1;  
    uint8_t LinearBufferTx2[1];
    LinearBuf_type LinearTx2;    
    uint8_t LinearBufferTx3[1];
    LinearBuf_type LinearTx3;  
    uint8_t LinearBufferTx4[1];
    LinearBuf_type LinearTx4;   
#endif
#if MCU_UART_QTY==2    
    #ifdef UART1_USE_AS_MODBUS
    uint8_t LinearBufferTx1[256]; 
    #else
    uint8_t LinearBufferTx1[1];
    #endif
    LinearBuf_type LinearTx1;
    #ifdef UART2_USE_AS_MODBUS
    uint8_t LinearBufferTx2[256];
    #else
    uint8_t LinearBufferTx2[1];
    #endif
    LinearBuf_type LinearTx2;  
    uint8_t LinearBufferTx3[1];
    LinearBuf_type LinearTx3; 
    uint8_t LinearBufferTx4[1];
    LinearBuf_type LinearTx4; 
#endif
#if MCU_UART_QTY==3    
    #ifdef UART1_USE_AS_MODBUS
    uint8_t LinearBufferTx1[256]; 
    #else
    uint8_t LinearBufferTx1[1];
    #endif
    LinearBuf_type LinearTx1;
    #ifdef UART2_USE_AS_MODBUS
    uint8_t LinearBufferTx2[256];
    #else
    uint8_t LinearBufferTx2[1];
    #endif
    LinearBuf_type LinearTx2;
    #ifdef UART3_USE_AS_MODBUS
    uint8_t LinearBufferTx3[256];
    #else
    uint8_t LinearBufferTx3[1];
    #endif
    LinearBuf_type LinearTx3;   
    uint8_t LinearBufferTx4[1];
    LinearBuf_type LinearTx4;     
#endif
#if MCU_UART_QTY==4    
    #ifdef UART1_USE_AS_MODBUS
    uint8_t LinearBufferTx1[256]; 
    #else
    uint8_t LinearBufferTx1[1];
    #endif
    LinearBuf_type LinearTx1;
    #ifdef UART2_USE_AS_MODBUS
    uint8_t LinearBufferTx2[256];
    #else
    uint8_t LinearBufferTx2[1];
    #endif
    LinearBuf_type LinearTx2;
    #ifdef UART3_USE_AS_MODBUS
    uint8_t LinearBufferTx3[256];
    #else
    uint8_t LinearBufferTx3[1];
    #endif
    LinearBuf_type LinearTx3;
    #ifdef UART4_USE_AS_MODBUS
    uint8_t LinearBufferTx4[256];
    #else
    uint8_t LinearBufferTx4[1];
    #endif
    LinearBuf_type LinearTx4;   
#endif   
#ifdef UART1_USE_AS_MODBUS   //XC8 ignore  __attribute__ ((weak))   
bool  UART1_TransmitDone(void)
{
    /*if (U1STAbits.TRMT)
        return true;
    else
        return false;*/
    return UART1_IsTxDone();
}
#else
#if !defined(UART1_H)
bool __attribute__ ((weak)) UART1_IsRxReady(void)
{
    return 0;
}
void __attribute__ ((weak))  UART1_ClearAll(void)
{
    
}
unsigned int __attribute__ ((weak)) UART1_User_WriteBuffer( uint8_t *buffer , unsigned int numbytes )
{
   return 0;
}
uint8_t __attribute__ ((weak)) UART1_Read( void)
{
    return 0;
}
bool __attribute__ ((weak)) UART1_TransmitDone(void)
{
    return true;
}  
#endif
#endif

#ifdef UART2_USE_AS_MODBUS   //XC8 ignore  __attribute__ ((weak))  
bool  UART2_TransmitDone(void)
{
    /*if (U2STAbits.TRMT)
        return true;
    else
        return false;*/
    return UART2_IsTxDone();
}
#else
#if !defined(UART2_H)
bool __attribute__ ((weak)) UART2_IsRxReady(void)
{
    return 0;
}
void __attribute__ ((weak))  UART2_ClearAll(void)
{
    
}
unsigned int __attribute__ ((weak)) UART2_User_WriteBuffer( uint8_t *buffer , unsigned int numbytes )
{
   return 0;
}
uint8_t __attribute__ ((weak)) UART2_Read( void)
{
    return 0;
}
bool __attribute__ ((weak)) UART2_TransmitDone(void)
{
    return true;
}  
#endif
#endif

#ifdef UART3_USE_AS_MODBUS   //XC8 ignore  __attribute__ ((weak))   
bool  UART3_TransmitDone(void)
{
    /*if (U3STAbits.TRMT)
        return true;
    else
        return false;*/
    return UART3_IsTxDone();
}
#else
#if !defined(UART3_H)
bool __attribute__ ((weak)) UART3_IsRxReady(void)
{
    return 0;
}
void __attribute__ ((weak))  UART3_ClearAll(void)
{
    
}
unsigned int __attribute__ ((weak)) UART3_User_WriteBuffer( uint8_t *buffer , unsigned int numbytes )
{
    return 0;
}
uint8_t __attribute__ ((weak)) UART3_Read( void)
{
    return 0;
}
bool __attribute__ ((weak)) UART3_TransmitDone(void)
{
    return true;
}   
#endif
#endif

#ifdef UART4_USE_AS_MODBUS   //XC8 ignore  __attribute__ ((weak))   
bool  UART4_TransmitDone(void)
{
    /*if (U4STAbits.TRMT)
        return true;
    else
        return false;*/
    return UART4_IsTxDone();
}
#else
#if !defined(UART4_H)
bool __attribute__ ((weak)) UART4_IsRxReady(void)
{
    return 0;
}
void __attribute__ ((weak))  UART4_ClearAll(void){
    
}
unsigned int __attribute__ ((weak)) UART4_User_WriteBuffer( uint8_t *buffer , unsigned int numbytes )
{
   return 0;
}
uint8_t __attribute__ ((weak)) UART4_Read( void)
{
    return 0;
}
bool __attribute__ ((weak)) UART4_TransmitDone(void)
{
    return true;
}  
#endif
#endif








































































































































































