mcontrol.h

00001 /***************************************************************************
00002                           mcontrol.h  - 
00003                           Modbus ISO/OSI level 7 protocol.
00004                              -------------------
00005     begin                : Thu March 28 2003
00006     email                : ngustavson@emacinc.com
00007  ***************************************************************************/
00008 #ifndef MCONTROL_H
00009 #define MCONTROL_H
00010 
00011 #include <GNU_defs.h>
00012 #include <realtime.h>
00013 #include <ser.h>
00014 
00015  
00016 //need to check on this number, 100 is just a guess
00017 #define MAXPDUDATASIZE 100
00018 //#define MAXPDUSIZE 100
00019 #define PDU_AVAIL 5 //number of PDUs allocatable
00020 
00021 
00022 typedef struct PDUstruct{
00023 u8 function;
00024 u8 data[MAXPDUDATASIZE];
00025 u8 attributes;
00026 int datasize;
00027 }PDUstruct;
00028 
00029 typedef struct PDU_Node{
00030 struct PDU_Node *previous;
00031 struct PDU_Node *next;
00032 PDUstruct PDU;
00033 }PDU_Node;
00034 
00035 typedef struct PDU_List{
00036 PDU_Node *head;
00037 PDU_Node *tail;
00038 int size;
00039 }PDU_List; 
00040 
00041 typedef struct modcounters
00042 {
00043 u16 bus_message;
00044 u16 bus_CRC_error;
00045 u16 slave_exception;
00046 u16 slave_message;
00047 u16 no_response;
00048 u16 slave_NAK;
00049 u16 slave_busy;
00050 u16 character_overrun;
00051 }modcounters;
00052 
00053 /*
00054 typedef struct mbconfig{
00055     u8 parity;    // parity
00056     u8 protocol;  // protocol ASCII or RTU
00057     u8 speed;     // baud rate
00058     u8 delim;    // the current delimiter   
00059     u16 address;   // address of the bus
00060 }mbconfig;
00061 */
00062 
00063 typedef struct Modbus_Port{
00064 //  mbconfig config;
00065     int parity;    // parity
00066     int protocol;  // protocol ASCII or RTU
00067     int speed;     // baud rate
00068     int delim;    // the current delimiter  
00069     int address;   // address of the bus
00070     int port;      // port num  
00071     int count;     // open count(should be 1 if open)
00072     int idle;      // idle count(not really used)
00073     int diagnostic;// diagnostic register
00074     u8 status;     // status flag register
00075 //  char newdelim; // holding register for delim changes
00076 /*
00077     int coilnum;
00078     int hregnum;
00079     int iregnum;
00080 */  
00081     modcounters counters;
00082     RTaskNode *RTUtasklet;
00083     RTIME t25;
00084     PDU_List PDUlist;
00085 }Modbus_Port;
00086 
00087 //#define MODBUS_PORTNUM PORT422
00088 
00089 #define MODBUS_PORTNUM NUM_COMPORTS
00090 
00091 //port status bits
00092 #define ADDCHANGE 1
00093 #define NEWDELIM  2
00094 //LISTEN_ONLY 4 defined as diagnostic sub
00095 #define CLEARLOG  8
00096 #define RESTART   0x10
00097 
00098 
00099 //PDU attributes
00100 #define BROADCAST 1
00101 
00102 //false function codes used for allocation
00103 #define NULL_FUNCTION        0
00104 #define ALLOCATED            1
00105 
00106 //function codes
00107 #define READ_COILS           1
00108 #define READ_DISCRETE_INPUTS 2
00109 #define READ_HOLDING_REGISTERS 3
00110 #define READ_INPUT_REGISTERS 4
00111     #define RI_DATANUM_BYTES 1
00112 
00113 #define WRITE_SINGLE_COIL    5
00114     #define ON_REQUEST   0xff00
00115     #define OFF_REQUEST  0
00116 
00117 #define WRITE_SINGLE_REGISTER 6
00118     #define WRITE_SING_RES_SIZE   4
00119 
00120 #define WRITE_MULTIPLE_COILS 15
00121 
00122 #define WRITE_MULTIPLE_REGISTERS 0x10
00123         #define WRITEM_RESPONSE_BYTES 4
00124         #define WR_DATASTART 5
00125 
00126 #define MASK_WRITE_REGISTER 0x16
00127     #define MASK_WRITE_SIZE 6
00128 
00129 #define DIAGNOSTICS 8   
00130     //sub function codes
00131     #define RETURN_QUERY 0
00132     #define RESTART_COMM 1
00133     #define RETURN_DIAGN 2
00134     #define CH_ASCII_IND 3
00135     #define LISTEN_ONLY  4
00136     #define CLEAR_COUNT  0x0a 
00137     #define RETURN_BUSM  0x0b
00138     #define COMM_ERR_CNT 0x0c
00139     #define BUS_EXCP_CNT 0x0d
00140     #define SLV_MSG_CNT  0x0e
00141     #define SLV_NO_RESP  0x0f
00142     #define SLV_NAK_CNT  0x10
00143     #define SLV_BUSY_CNT 0x11
00144     #define RETRN_OVERRN 0x12
00145     #define CLEAR_OVERRN 0x14
00146 
00147 //counter register defines
00148 
00149 #define STARTCOUNT   1
00150 #define TRIGBITS     6
00151 #define ASTOPBIT     8
00152 #define ARELOADBIT   0x10
00153 #define COUNTENABLE  0x80
00154 
00155 //config_register_defines
00156 #define BAUD_RATE_BIT       0x01
00157 #define PARITY_TYPE_BIT     0x08
00158 #define PARITY_ON_BIT       0x10
00159 #define PROTOCOL_BIT        0x80
00160 
00161 #define MODBUS_DEFAULT BAUD_RATE_BIT|PARITY_TYPE_BIT
00162 #define MODBUS_ADDRESS_DEFAULT 2
00163 
00164 //exception codes
00165 #define ILLEGAL_FUNCTION     1
00166 #define ILLEGAL_DATA_ADDRESS 2
00167 #define ILLEGAL_DATA_VALUE   3
00168 
00169 PDU_Node __attribute__((far)) *GetPDUnode(void);
00170 void __attribute__((far)) DumpPDUQueue(Modbus_Port *modport);
00171 int __attribute__((far)) Dump_Frames(Modbus_Port *modport);
00172 int __attribute__((far)) PDU_exe(Modbus_Port *modport, PDUstruct *PDU);
00173 int __attribute__((far)) EnQueue_PDU(PDU_Node *newnode, Modbus_Port *modport);
00174 PDUstruct __attribute__((far)) *DeQueue_PDU(Modbus_Port *modport);
00175 Modbus_Port __attribute__((far)) *Modbus_Device_Open(int port, int speed,int address,
00176      int parity, int protocol,char delim);
00177 int __attribute__((far)) Modbus_Diagnostic(Modbus_Port *modport,u8 *data, int datasize);
00178 int  __attribute__((far)) Modbus_Slave_Control(Modbus_Port *modport);
00179 int  __attribute__((far)) Modbus_Device_Close(Modbus_Port *modport);
00180 
00181 /***************************************************************
00182  * Modbus Restart
00183  * Used to automate the restart of a modbus port
00184  * @param port Modbus_Port to start
00185  * @return pointer to the Modbus_Port structure
00186  */
00187 static inline Modbus_Port *Modport_Restart(Modbus_Port *port){
00188     return(Modbus_Device_Open(port->port,port->speed,
00189         port->address,port->parity,port->protocol,port->delim));
00190 }
00191 
00192 #include <protocol/mserial.h>
00193 static inline void print_modbus_state(int comport,Modbus_Port *port){
00194     ser_printf(comport,"address: %u\r\n",(long)port->address);
00195     ser_printf(comport,"protocol: %s\r\n",(port->protocol==RTU_PROTOCOL)?"RTU":"ASCII");
00196     ser_printf(comport,"speed: %u\r\n",(long)port->speed);
00197     ser_printf(comport,"delim: %x\r\n",(long)port->delim);
00198     
00199     if(port->parity==NO_PARITY)
00200         ser_printf(comport,"parity: OFF\r\n");
00201     else    
00202         ser_printf(comport,"parity: %s\r\n",(port->parity==EVEN_PARITY)?"EVEN":"ODD");
00203 }
00204 
00205 #endif
00206 
00207 
00208 
00209 
00210 
00211 

Generated on Fri Mar 3 17:25:45 2006 for EMAC-HCS12-SDK by  doxygen 1.4.6