00001 #ifndef _MFUNCTIONS_H_
00002 #define _MFUNCTIONS_H_
00003
00004 #include <GNU_defs.h>
00005 #include <init.h>
00006 #include <protocol/mcontrol.h>
00007
00008
00009 typedef u16 mvregister;
00010
00011 #define VREGSIZE (sizeof(mvregister))
00012
00013 typedef int __attribute__((far))(*mvregister_function)(mvregister *);
00014
00015
00016 typedef struct mvregister_array{
00017 mvregister_function *function;
00018 int length;
00019 }mvregister_array;
00020
00021 static inline mvregister_array *set_array(mvregister_array *target,
00022 mvregister_function *source, int length){
00023 target->function = source;
00024 target->length = length;
00025 return target;
00026
00027 }
00028
00029
00030
00031
00032
00033 static inline int rvregister(mvregister_array *regarray,int address,mvregister *data){
00034 mvregister ret;
00035 int status;
00036
00037 if((address>=regarray->length)||(address<0))
00038 return ILLEGAL_DATA_ADDRESS;
00039
00040 status = regarray->function[address](&ret);
00041 GENDEBUG("mvregister %u: = %u\r\n",(long)address,(long)ret);
00042
00043
00044 *data = (mvregister)ret;
00045 GENDEBUG("bent data = %u\r\n",(long)*data);
00046 return status;
00047 }
00048
00049
00050
00051
00052
00053 static inline int wvregister(mvregister_array *regarray,
00054 int address,mvregister *data){
00055
00056 mvregister bentdata = bend2int((u8 *)data);
00057
00058 if((address>=regarray->length)||(address<0))
00059 return ILLEGAL_DATA_ADDRESS;
00060
00061 GENDEBUG("writing %u: = %u\r\n",(long)address,(long)bentdata);
00062
00063 return regarray->function[address](&bentdata);
00064 }
00065
00066
00067 static inline void ser_dump_array(int comport,mvregister_array *array){
00068 int regnum;
00069 mvregister data;
00070 for(regnum=0;regnum<array->length;regnum++){
00071
00072 array->function[regnum](&data);
00073 ser_printf(comport,"reg %u: %u\r\n",(long)regnum,(long)(int)data);
00074 }
00075
00076 }
00077
00078 static inline void ser_dump_array_hex(int comport,mvregister_array *array){
00079 int regnum;
00080 mvregister data;
00081 for(regnum=0;regnum<array->length;regnum++){
00082
00083 array->function[regnum](&data);
00084 ser_printf(comport,"reg %u: %x\r\n",(long)regnum,(long)data);
00085 }
00086
00087 }
00088
00089
00090
00091
00092
00097 static inline int mwvregister(mvregister_array *regarrayr,
00098 mvregister_array *regarrayw,int address,
00099 mvregister and, mvregister or){
00100
00101 int status;
00102 mvregister value;
00103 if((status = rvregister(regarrayr,
00104 address,(&value))))
00105 return status;
00106
00107 value = (value|or)∧
00108
00109 return(status=wvregister(regarrayw,address,&value));
00110 }
00111
00112 mvregister_array __attribute__((far)) *MWR_Array(mvregister_function *WR, int length);
00113 mvregister_array __attribute__((far)) *MWC_Array(mvregister_function *WCB, int length);
00114 mvregister_array __attribute__((far)) *MIRR_Array(mvregister_function *IRR, int length);
00115 mvregister_array __attribute__((far)) *MHRR_Array(mvregister_function *HRR, int length);
00116 mvregister_array __attribute__((far)) *MICR_Array(mvregister_function *ICR, int length);
00117
00118 #define MWR_SET(array) MWR_Array((mvregister_function *)array, sizeof(array)/sizeof(mvregister_function))
00119 #define MWC_SET(array) MWC_Array((mvregister_function *)array, sizeof(array)/sizeof(mvregister_function))
00120 #define MIRR_SET(array) MIRR_Array((mvregister_function *)array, sizeof(array)/sizeof(mvregister_function))
00121 #define MHRR_SET(array) MHRR_Array((mvregister_function *)array, sizeof(array)/sizeof(mvregister_function))
00122 #define MICR_SET(array) MICR_Array((mvregister_function *)array, sizeof(array)/sizeof(mvregister_function))
00123
00124 int __attribute__((far)) Write_regs(int address,int reg_num, u8 *data);
00125 int __attribute__((far)) Write_Coils(int address,int coilnum, u8 *data);
00126 int __attribute__((far)) Read_Iregs(int address,int reg_num, u8 *dataout);
00127 int __attribute__((far)) Read_Hregs(int address,int reg_num, u8 *dataout);
00128 int __attribute__((far)) Read_Coils(int address,int coilnum, u8 *dataout);
00129 int __attribute__((far)) Switch_Coil(int coil,int command);
00130 int __attribute__((far)) Modbus_Diagnostic(Modbus_Port *modport,u8 *data, int datasize);
00131 int __attribute__((far)) Mask_Write_Register(mvregister address, mvregister and, mvregister or);
00132 int __attribute__((far)) Read_Coils(int address,int coilnum, u8 *dataout);
00133
00134
00135 #endif //_FUNCTIONS_H_