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)
*/
#include "simstruc.h"
+#include <stdint.h>
+
+#ifndef WITHOUT_HW
#include "mf624_SIMULINK.h"
+#endif /*WITHOUT_HW*/
/* Error handling
*/
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 */
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*/
}
*/
static void mdlTerminate(SimStruct *S)
{
+ #ifndef WITHOUT_HW
mf624_done(S);
+ #endif /*WITHOUT_HW*/
}
* its associated macro definitions.
*/
#include "simstruc.h"
+
+#ifndef WITHOUT_HW
#include "mf624_SIMULINK.h"
+#endif /*WITHOUT_HW*/
/* Error handling
* --------------
*/
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 */
//mf624_state_t* mfst = ssGetPWorkValue(S,0);
int out;
+ #ifndef WITHOUT_HW
if (mf624_check(S) != 0)
return;
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*/
}
*/
static void mdlTerminate(SimStruct *S)
{
+ #ifndef WITHOUT_HW
//mf624_state_t* mfst = ssGetPWorkValue(S,0);
if (mf624_check(NULL) != 0)
return;
MFST2REG(mfst, 0, GPIOC_reg));
mf624_done();
+ #endif /*WITHOUT_HW*/
}
* its associated macro definitions.
*/
#include "simstruc.h"
+
+#ifndef WITHOUT_HW
#include "mf624_SIMULINK.h"
+#endif /*WITHOUT_HW*/
/* Error handling
* --------------
*/
static void mdlStart(SimStruct *S)
{
+ #ifndef WITHOUT_HW
if (mf624_init(NULL) != 0)
- return;
+ return;
+ #endif /*WITHOUT_HW*/
}
#endif /* MDL_START */
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*/
}
*/
static void mdlTerminate(SimStruct *S)
{
- mf624_done();
+ #ifndef WITHOUT_HW
+ mf624_done();
+ #endif /*WITHOUT_HW*/
}
* its associated macro definitions.
*/
#include "simstruc.h"
+
+#ifndef WITHOUT_HW
#include "mf624_SIMULINK.h"
+#endif /*WITHOUT_HW*/
/* Error handling
* --------------
*/
static void mdlStart(SimStruct *S)
{
+ #ifndef WITHOUT_HW
if (mf624_init(NULL) != 0)
return;
mfst->DOut=0;
-
+ #endif /*WITHOUT_HW*/
}
#endif /* MDL_START */
{
const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
+ #ifndef WITHOUT_HW
if (mf624_check(S) != 0)
return;
}
mf624_write16(mfst->DOut, MFST2REG(mfst, 2, DOUT_reg));
+ #endif /*WITHOUT_HW*/
}
*/
static void mdlTerminate(SimStruct *S)
{
- mf624_done();
+ #ifndef WITHOUT_HW
+ mf624_done();
+ #endif /*WITHOUT_HW*/
}
* its associated macro definitions.
*/
#include "simstruc.h"
+
+#ifndef WITHOUT_HW
#include "mf624_SIMULINK.h"
static const int32_T irc_channel2reg[] = {
IRC2_reg,
IRC3_reg
};
+#endif /*WITHOUT_HW*/
/* Error handling
* --------------
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");
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");
}
int_T reset_mode;
int_T reset_mode_save;
+ #ifndef WITHOUT_HW
if (mf624_check(S) != 0)
return;
IRC_mode_change(mfst, mask, reset_mode);
IRC_mode_change(mfst, mask, reset_mode_save & mask);
}
+ #endif /*WITHOUT_HW*/
}
#endif /* MDL_INITIALIZE_CONDITIONS */
int_T filter;
int_T mask;
+ #ifndef WITHOUT_HW
if (mf624_init(NULL) != 0)
return;
IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
+ #endif /*WITHOUT_HW*/
+
mdlInitializeConditions(S);
}
#endif /* MDL_START */
{
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*/
}
int_T shift;
int_T inp_num = 0;
+ #ifndef WITHOUT_HW
if (mf624_check(S) != 0)
return;
shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
}
+ #endif /*WITHOUT_HW*/
}
#endif /* MDL_UPDATE */
*/
static void mdlTerminate(SimStruct *S)
{
+ #ifndef WITHOUT_HW
mf624_done();
+ #endif /*WITHOUT_HW*/
}
* 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;
{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
* --------------
{
int32_T ctr_mode;
+ #ifndef WITHOUT_HW
if (mf624_init(NULL) != 0)
return;
/* 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 */
uint32_T T1_uint;
uint32_T T2_uint;
+ #ifndef WITHOUT_HW
if (mf624_check(S) != 0)
return;
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*/
}
{
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);
}
mf624_done();
+ #endif /*WITHOUT_HW*/
}
* 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;
{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
* --------------
* 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)
mf624_write16(mfst->DOut, MFST2REG(mfst, 2, DOUT_reg));
}
+#endif /*WITHOUT_HW*/
/*====================*
* S-function methods *
{
int32_T ctr_mode;
+ #ifndef WITHOUT_HW
if (mf624_init(NULL) != 0)
return;
/* 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 */
uint32_T T2_uint;
int_T new_mode;
+ #ifndef WITHOUT_HW
if (mf624_check(S) != 0)
return;
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*/
}
{
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);
}
mf624_done();
+ #endif /*WITHOUT_HW*/
}
*/
#include "simstruc.h"
+#ifndef WITHOUT_HW
#include "mf624_SIMULINK.h"
+#endif /*WITHOUT_HW*/
/* Error handling
* --------------
*/
static void mdlStart(SimStruct *S)
{
+ #ifndef WITHOUT_HW
if (mf624_init(NULL) != 0)
return;
/*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 */
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*/
}
*/
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*/
}