From: Pavel Pisa Date: Sat, 22 Feb 2014 13:03:39 +0000 (+0100) Subject: Add build mode WITHOUT_HW to edit model without MF624 hardware access. X-Git-Url: http://rtime.felk.cvut.cz/gitweb/mf624-simulink.git/commitdiff_plain/13cde8a33f64c7b6d64c456a133efe6525e8a4a7 Add build mode WITHOUT_HW to edit model without MF624 hardware access. Signed-off-by: Pavel Pisa --- diff --git a/Makefile b/Makefile index 620da70..7b294b3 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,20 @@ MEXFLAGS ?= -f ./mexopts.sh -g +# Build MF624 MEX functions without dependency on hardware access +#WITHOUT_HW=y + +ifeq ($(WITHOUT_HW),y) +MEXFLAGS += -DWITHOUT_HW=1 +else +MF624_COMMON_C_FILES=mf624_SIMULINK.c +endif + all: - mex $(MEXFLAGS) sfAnalogInput.c mf624_SIMULINK.c - mex $(MEXFLAGS) sfAnalogOutput.c mf624_SIMULINK.c - mex $(MEXFLAGS) sfDigitalOutput.c mf624_SIMULINK.c - mex $(MEXFLAGS) sfDigitalInput.c mf624_SIMULINK.c - mex $(MEXFLAGS) sfReadPWM.c mf624_SIMULINK.c - mex $(MEXFLAGS) sfIRCInput.c mf624_SIMULINK.c - mex $(MEXFLAGS) sfPWMOutput.c mf624_SIMULINK.c - mex $(MEXFLAGS) sfPWMwDirOutput.c mf624_SIMULINK.c + mex $(MEXFLAGS) sfAnalogInput.c $(MF624_COMMON_C_FILES) + mex $(MEXFLAGS) sfAnalogOutput.c $(MF624_COMMON_C_FILES) + mex $(MEXFLAGS) sfDigitalOutput.c $(MF624_COMMON_C_FILES) + mex $(MEXFLAGS) sfDigitalInput.c $(MF624_COMMON_C_FILES) + mex $(MEXFLAGS) sfReadPWM.c $(MF624_COMMON_C_FILES) + mex $(MEXFLAGS) sfIRCInput.c $(MF624_COMMON_C_FILES) + mex $(MEXFLAGS) sfPWMOutput.c $(MF624_COMMON_C_FILES) + mex $(MEXFLAGS) sfPWMwDirOutput.c $(MF624_COMMON_C_FILES) diff --git a/sfAnalogInput.c b/sfAnalogInput.c index fe72ebf..70e4ca9 100644 --- a/sfAnalogInput.c +++ b/sfAnalogInput.c @@ -46,7 +46,11 @@ */ #include "simstruc.h" +#include + +#ifndef WITHOUT_HW #include "mf624_SIMULINK.h" +#endif /*WITHOUT_HW*/ /* Error handling @@ -175,26 +179,27 @@ static void mdlInitializeSampleTimes(SimStruct *S) */ static void mdlStart(SimStruct *S) { + + #ifndef WITHOUT_HW if (mf624_init(NULL) != 0) return; - + #endif /*WITHOUT_HW*/ + int ADCCMask = (int)MASK_PRM(S); if(ADCCMask > 255 || ADCCMask < 0) { ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255"); } - + int ADCChannels = __builtin_popcount((uint32_t)ADCCMask); //Counts number of set bits in ADCCMask - + ssSetIWorkValue(S, 0, ADCChannels); - + #ifndef WITHOUT_HW mfst->ADC_enabled = ADCCMask; mfst->ADC_enabled &= 0xFF; - + mf624_write16(mfst->ADC_enabled, MFST2REG(mfst, 2, ADCTRL_reg)); - - - + #endif /*WITHOUT_HW*/ } #endif /* MDL_START */ @@ -212,35 +217,41 @@ static void mdlOutputs(SimStruct *S, int_T tid) real_T* y[ADCChannels]; int i; int res,res1; - + #ifndef WITHOUT_HW if (mf624_check(S) != 0) - return; + return; // Activate trigger to start conversion - mf624_read16(MFST2REG(mfst, 2, ADSTART_reg)); - + mf624_read16(MFST2REG(mfst, 2, ADSTART_reg)); + #endif /*WITHOUT_HW*/ + for(i=0; i < ADCChannels;i++){ y[i]=ssGetOutputPortSignal(S,i); } - + + #ifndef WITHOUT_HW // Check if conversion has finished - while((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg)) & GPIOC_EOLC_mask)) { - for (i = 0; i < 1000; i++) {} // small wait - } - - - for(i=1; i < ADCChannels;i+=2){ + while((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg)) & GPIOC_EOLC_mask)) { + for (i = 0; i < 1000; i++) {} // small wait + } + + for(i=1; i < ADCChannels;i+=2){ res = mf624_read32(MFST2REG(mfst, 2, ADDATA0_reg)); res1= res >> 16; res = res & 0xFFFF; *y[i-1] = (real_T) (10.0 * ((int16_t) (res << 2)) / (double) 0x8000); *y[i] = (real_T) (10.0 * ((int16_t) (res1 << 2)) / (double) 0x8000); } - + if(i == ADCChannels){ res = mf624_read16(MFST2REG(mfst, 2, ADDATA0_reg)); *y[ADCChannels-1]=(real_T)(10.0 * ((int16_t) (res << 2)) / (double) 0x8000); } + #else /*WITHOUT_HW*/ + for(i=0; i < ADCChannels; i++){ + *y[i] = 0; + } + #endif /*WITHOUT_HW*/ } @@ -283,7 +294,9 @@ static void mdlOutputs(SimStruct *S, int_T tid) */ static void mdlTerminate(SimStruct *S) { + #ifndef WITHOUT_HW mf624_done(S); + #endif /*WITHOUT_HW*/ } diff --git a/sfAnalogOutput.c b/sfAnalogOutput.c index 3056060..d2d0ad0 100644 --- a/sfAnalogOutput.c +++ b/sfAnalogOutput.c @@ -47,7 +47,10 @@ * its associated macro definitions. */ #include "simstruc.h" + +#ifndef WITHOUT_HW #include "mf624_SIMULINK.h" +#endif /*WITHOUT_HW*/ /* Error handling * -------------- @@ -168,9 +171,11 @@ static void mdlInitializeSampleTimes(SimStruct *S) */ static void mdlStart(SimStruct *S) { + #ifndef WITHOUT_HW if (mf624_init(NULL) != 0) return; DAC_enable(mfst); + #endif /*WITHOUT_HW*/ //ssSetPWorkValue(S, 0, mfst); } #endif /* MDL_START */ @@ -188,6 +193,7 @@ static void mdlOutputs(SimStruct *S, int_T tid) //mf624_state_t* mfst = ssGetPWorkValue(S,0); int out; + #ifndef WITHOUT_HW if (mf624_check(S) != 0) return; @@ -201,6 +207,7 @@ static void mdlOutputs(SimStruct *S, int_T tid) out = (int) ((u[0] + 10) * 8192 / 10 + 0.5); } mf624_write16(out, MFST2REG(mfst, 2, dac_channel2reg[(int)CHNL_PRM(S)-1])); + #endif /*WITHOUT_HW*/ } @@ -243,6 +250,7 @@ static void mdlOutputs(SimStruct *S, int_T tid) */ static void mdlTerminate(SimStruct *S) { + #ifndef WITHOUT_HW //mf624_state_t* mfst = ssGetPWorkValue(S,0); if (mf624_check(NULL) != 0) return; @@ -253,6 +261,7 @@ static void mdlTerminate(SimStruct *S) MFST2REG(mfst, 0, GPIOC_reg)); mf624_done(); + #endif /*WITHOUT_HW*/ } diff --git a/sfDigitalInput.c b/sfDigitalInput.c index d5fca66..a6294c6 100644 --- a/sfDigitalInput.c +++ b/sfDigitalInput.c @@ -46,7 +46,10 @@ * its associated macro definitions. */ #include "simstruc.h" + +#ifndef WITHOUT_HW #include "mf624_SIMULINK.h" +#endif /*WITHOUT_HW*/ /* Error handling * -------------- @@ -159,8 +162,10 @@ static void mdlInitializeSampleTimes(SimStruct *S) */ static void mdlStart(SimStruct *S) { + #ifndef WITHOUT_HW if (mf624_init(NULL) != 0) - return; + return; + #endif /*WITHOUT_HW*/ } #endif /* MDL_START */ @@ -174,16 +179,20 @@ static void mdlInitializeSampleTimes(SimStruct *S) static void mdlOutputs(SimStruct *S, int_T tid) { real_T *y; - uint16_t din; + uint_T din; + + y = (real_T *)ssGetOutputPortSignal(S, 0); + #ifndef WITHOUT_HW if (mf624_check(S) != 0) return; - y = (real_T *)ssGetOutputPortSignal(S, 0); - din = mf624_read16(MFST2REG(mfst, 2, DIN_reg)); *y = din & (1 << (int)(CHNL_PRM(S)-1)) ? 0.0 : 1.0; + #else /*WITHOUT_HW*/ + *y = 0; + #endif /*WITHOUT_HW*/ } @@ -226,7 +235,9 @@ static void mdlOutputs(SimStruct *S, int_T tid) */ static void mdlTerminate(SimStruct *S) { - mf624_done(); + #ifndef WITHOUT_HW + mf624_done(); + #endif /*WITHOUT_HW*/ } diff --git a/sfDigitalOutput.c b/sfDigitalOutput.c index b768d9c..f3fd889 100644 --- a/sfDigitalOutput.c +++ b/sfDigitalOutput.c @@ -47,7 +47,10 @@ * its associated macro definitions. */ #include "simstruc.h" + +#ifndef WITHOUT_HW #include "mf624_SIMULINK.h" +#endif /*WITHOUT_HW*/ /* Error handling * -------------- @@ -168,10 +171,11 @@ static void mdlInitializeSampleTimes(SimStruct *S) */ static void mdlStart(SimStruct *S) { + #ifndef WITHOUT_HW if (mf624_init(NULL) != 0) return; mfst->DOut=0; - + #endif /*WITHOUT_HW*/ } #endif /* MDL_START */ @@ -186,6 +190,7 @@ static void mdlOutputs(SimStruct *S, int_T tid) { const real_T *u = (const real_T*) ssGetInputPortSignal(S,0); + #ifndef WITHOUT_HW if (mf624_check(S) != 0) return; @@ -199,6 +204,7 @@ static void mdlOutputs(SimStruct *S, int_T tid) } mf624_write16(mfst->DOut, MFST2REG(mfst, 2, DOUT_reg)); + #endif /*WITHOUT_HW*/ } @@ -241,7 +247,9 @@ static void mdlOutputs(SimStruct *S, int_T tid) */ static void mdlTerminate(SimStruct *S) { - mf624_done(); + #ifndef WITHOUT_HW + mf624_done(); + #endif /*WITHOUT_HW*/ } diff --git a/sfIRCInput.c b/sfIRCInput.c index b9d4655..86ae04d 100644 --- a/sfIRCInput.c +++ b/sfIRCInput.c @@ -75,6 +75,8 @@ * its associated macro definitions. */ #include "simstruc.h" + +#ifndef WITHOUT_HW #include "mf624_SIMULINK.h" static const int32_T irc_channel2reg[] = { @@ -83,6 +85,7 @@ static const int32_T irc_channel2reg[] = { IRC2_reg, IRC3_reg }; +#endif /*WITHOUT_HW*/ /* Error handling * -------------- @@ -127,6 +130,7 @@ static void mdlCheckParameters(SimStruct *S) ssSetErrorStatus(S, "Ts has to be positive or -1 for automatic step"); if ((PRM_CHANNEL(S) < 0) || (PRM_CHANNEL(S) > 3)) ssSetErrorStatus(S, "valid IRC channel is 0, 1, 2, or 3"); + #ifndef WITHOUT_HW if ((PRM_COUNTER_MODE(S) < 0) || PRM_COUNTER_MODE(S) > __mfld2val(IRCCTRL_IRC0MODE_mask, IRCCTRL_IRC0MODE_mask)) ssSetErrorStatus(S, "Counter Mode out of valid range"); @@ -141,6 +145,7 @@ static void mdlCheckParameters(SimStruct *S) if ((PRM_DIGITAL_FILTER(S) < 0) || PRM_DIGITAL_FILTER(S) > __mfld2val(IRCCTRL_IRC0FILTER_mask, IRCCTRL_IRC0FILTER_mask)) ssSetErrorStatus(S, "Digital Filter out of valid range"); + #endif /*WITHOUT_HW*/ if ((PRM_RESET_AT_STARTUP(S) != 0) && (PRM_RESET_AT_STARTUP(S) != 1)) ssSetErrorStatus(S, "Reset at startup can be only 0 or 1"); } @@ -249,6 +254,7 @@ static void mdlInitializeConditions(SimStruct *S) int_T reset_mode; int_T reset_mode_save; + #ifndef WITHOUT_HW if (mf624_check(S) != 0) return; @@ -262,6 +268,7 @@ static void mdlInitializeConditions(SimStruct *S) IRC_mode_change(mfst, mask, reset_mode); IRC_mode_change(mfst, mask, reset_mode_save & mask); } + #endif /*WITHOUT_HW*/ } #endif /* MDL_INITIALIZE_CONDITIONS */ @@ -282,6 +289,7 @@ static void mdlStart(SimStruct *S) int_T filter; int_T mask; + #ifndef WITHOUT_HW if (mf624_init(NULL) != 0) return; @@ -309,6 +317,8 @@ static void mdlStart(SimStruct *S) IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift); + #endif /*WITHOUT_HW*/ + mdlInitializeConditions(S); } #endif /* MDL_START */ @@ -324,10 +334,14 @@ static void mdlOutputs(SimStruct *S, int_T tid) { int32_T *y = ssGetOutputPortSignal(S,0); + #ifndef WITHOUT_HW if (mf624_check(S) != 0) return; y[0] = mf624_read32(MFST2REG(mfst, 4, irc_channel2reg[IWORK_CHANNEL(S)])); + #else /*WITHOUT_HW*/ + y[0] = 0; + #endif /*WITHOUT_HW*/ } @@ -347,6 +361,7 @@ static void mdlUpdate(SimStruct *S, int_T tid) int_T shift; int_T inp_num = 0; + #ifndef WITHOUT_HW if (mf624_check(S) != 0) return; @@ -376,6 +391,7 @@ static void mdlUpdate(SimStruct *S, int_T tid) shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT; IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift); } + #endif /*WITHOUT_HW*/ } #endif /* MDL_UPDATE */ @@ -403,7 +419,9 @@ static void mdlUpdate(SimStruct *S, int_T tid) */ static void mdlTerminate(SimStruct *S) { + #ifndef WITHOUT_HW mf624_done(); + #endif /*WITHOUT_HW*/ } diff --git a/sfPWMOutput.c b/sfPWMOutput.c index 605ff76..787dba1 100644 --- a/sfPWMOutput.c +++ b/sfPWMOutput.c @@ -71,10 +71,15 @@ enum {CTR_LAST_MODE_OUT0, CTR_LAST_MODE_OUT1, CTR_LAST_MODE_PWM}; * its associated macro definitions. */ #include "simstruc.h" + + +#ifndef WITHOUT_HW #include "mf624_SIMULINK.h" +#endif /*WITHOUT_HW*/ #define CTR_MAX_PWM_CHANNEL 3 +#ifndef WITHOUT_HW typedef struct { int32_T STATUS_reg; int32_T MODE_reg; @@ -91,6 +96,7 @@ static const ctr_channel_regs_t ctr_channel2regs[] = { {CTR3STATUS_reg, CTR3MODE_reg, CTR3_reg, CTR3A_reg, CTR3B_reg, CTRXCTRL_reg}, {CTR4STATUS_reg, CTR4MODE_reg, CTR4_reg, CTR4A_reg, -1, CTRXCTRL_reg} }; +#endif /*WITHOUT_HW*/ /* Error handling * -------------- @@ -247,6 +253,7 @@ static void mdlStart(SimStruct *S) { int32_T ctr_mode; + #ifndef WITHOUT_HW if (mf624_init(NULL) != 0) return; @@ -263,6 +270,7 @@ static void mdlStart(SimStruct *S) /* Force output low during startup */ ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO); mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg)); + #endif /*WITHOUT_HW*/ } #endif /* MDL_START */ @@ -284,6 +292,7 @@ static void mdlOutputs(SimStruct *S, int_T tid) uint32_T T1_uint; uint32_T T2_uint; + #ifndef WITHOUT_HW if (mf624_check(S) != 0) return; @@ -363,6 +372,7 @@ static void mdlOutputs(SimStruct *S, int_T tid) ctr_ctrl <<= IWORK_CHANNEL(S) * CTRXCTRL_CHANNEL_SHIFT; mf624_write32(ctr_ctrl, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].CTRL_reg)); } + #endif /*WITHOUT_HW*/ } @@ -407,6 +417,7 @@ static void mdlTerminate(SimStruct *S) { int32_T ctr_mode; + #ifndef WITHOUT_HW if (mf624_check(S) == 0) { /* Force output low when finished */ ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO); @@ -414,6 +425,7 @@ static void mdlTerminate(SimStruct *S) } mf624_done(); + #endif /*WITHOUT_HW*/ } diff --git a/sfPWMwDirOutput.c b/sfPWMwDirOutput.c index 3751f62..24bf21a 100644 --- a/sfPWMwDirOutput.c +++ b/sfPWMwDirOutput.c @@ -76,10 +76,14 @@ enum {CTR_MODE_ZERO = 0, CTR_MODE_PLUS_PWM = 1, CTR_MODE_PLUS_FULL = 2, * its associated macro definitions. */ #include "simstruc.h" + +#ifndef WITHOUT_HW #include "mf624_SIMULINK.h" +#endif /*WITHOUT_HW*/ #define CTR_MAX_PWM_CHANNEL 3 +#ifndef WITHOUT_HW typedef struct { int32_T STATUS_reg; int32_T MODE_reg; @@ -96,6 +100,7 @@ static const ctr_channel_regs_t ctr_channel2regs[] = { {CTR3STATUS_reg, CTR3MODE_reg, CTR3_reg, CTR3A_reg, CTR3B_reg, CTRXCTRL_reg}, {CTR4STATUS_reg, CTR4MODE_reg, CTR4_reg, CTR4A_reg, -1, CTRXCTRL_reg} }; +#endif /*WITHOUT_HW*/ /* Error handling * -------------- @@ -121,6 +126,7 @@ static const ctr_channel_regs_t ctr_channel2regs[] = { * See matlabroot/simulink/src/sfuntmpl_doc.c for more details. */ +#ifndef WITHOUT_HW static void fncSetDirOutput(SimStruct *S, int_T direction_mode) { if (mf624_check(S) != 0) @@ -135,6 +141,7 @@ static void fncSetDirOutput(SimStruct *S, int_T direction_mode) mf624_write16(mfst->DOut, MFST2REG(mfst, 2, DOUT_reg)); } +#endif /*WITHOUT_HW*/ /*====================* * S-function methods * @@ -269,6 +276,7 @@ static void mdlStart(SimStruct *S) { int32_T ctr_mode; + #ifndef WITHOUT_HW if (mf624_init(NULL) != 0) return; @@ -286,6 +294,7 @@ static void mdlStart(SimStruct *S) /* Force output low during startup */ ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO); mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg)); + #endif /*WITHOUT_HW*/ } #endif /* MDL_START */ @@ -308,6 +317,7 @@ static void mdlOutputs(SimStruct *S, int_T tid) uint32_T T2_uint; int_T new_mode; + #ifndef WITHOUT_HW if (mf624_check(S) != 0) return; @@ -410,6 +420,7 @@ static void mdlOutputs(SimStruct *S, int_T tid) ctr_ctrl <<= IWORK_CHANNEL(S) * CTRXCTRL_CHANNEL_SHIFT; mf624_write32(ctr_ctrl, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].CTRL_reg)); } + #endif /*WITHOUT_HW*/ } @@ -454,6 +465,7 @@ static void mdlTerminate(SimStruct *S) { int32_T ctr_mode; + #ifndef WITHOUT_HW if (mf624_check(S) == 0) { /* Force output low when finished */ ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO); @@ -461,6 +473,7 @@ static void mdlTerminate(SimStruct *S) } mf624_done(); + #endif /*WITHOUT_HW*/ } diff --git a/sfReadPWM.c b/sfReadPWM.c index 33399ae..979f046 100644 --- a/sfReadPWM.c +++ b/sfReadPWM.c @@ -46,7 +46,9 @@ */ #include "simstruc.h" +#ifndef WITHOUT_HW #include "mf624_SIMULINK.h" +#endif /*WITHOUT_HW*/ /* Error handling * -------------- @@ -162,6 +164,7 @@ static void mdlInitializeSampleTimes(SimStruct *S) */ static void mdlStart(SimStruct *S) { + #ifndef WITHOUT_HW if (mf624_init(NULL) != 0) return; @@ -185,8 +188,7 @@ static void mdlInitializeSampleTimes(SimStruct *S) /*Start counters 0,1,2, tehy are gated with their inputs so no worries about premature start*/ mf624_write32(CTR_START,MFST2REG(mfst,4,CTRXCTRL_reg)); - - + #endif /*WITHOUT_HW*/ } #endif /* MDL_START */ @@ -204,25 +206,28 @@ static void mdlOutputs(SimStruct *S, int_T tid) real_T *y2 = ssGetOutputPortSignal(S,2); unsigned int period; unsigned int c0,c1,c2,c4; - + #ifndef WITHOUT_HW if (mf624_check(S) != 0) - return; - + return; + c0 = mf624_read32(MFST2REG(mfst,4,CTR0_reg)); c1 = mf624_read32(MFST2REG(mfst,4,CTR1_reg)); c2 = mf624_read32(MFST2REG(mfst,4,CTR2_reg)); c4 = mf624_read32(MFST2REG(mfst,4,CTR4_reg)); - + period = (unsigned int)(c4-(unsigned int)ssGetIWorkValue(S,3)); - + y0[0] = (real_T)(c0-(unsigned int)ssGetIWorkValue(S,0))/(real_T)period; y1[0] = (real_T)(c1-(unsigned int)ssGetIWorkValue(S,1))/(real_T)period; y2[0] = (real_T)(c2-(unsigned int)ssGetIWorkValue(S,2))/(real_T)period; - + ssSetIWorkValue(S,0,c0); ssSetIWorkValue(S,1,c1); ssSetIWorkValue(S,2,c2); ssSetIWorkValue(S,3,c4); + #else /*WITHOUT_HW*/ + y0[0] = y1[0] = y2[0] = 0; + #endif /*WITHOUT_HW*/ } @@ -265,11 +270,13 @@ static void mdlOutputs(SimStruct *S, int_T tid) */ static void mdlTerminate(SimStruct *S) { + #ifndef WITHOUT_HW if (mf624_check(NULL) != 0) - return; + return; mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL_reg)); mf624_done(); + #endif /*WITHOUT_HW*/ }