From 94af05c6b1740a44b2de49c2a4796259b8109ed7 Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Sat, 15 Feb 2014 09:28:18 +0100 Subject: [PATCH] IRC inputs support implemented. sfIRCInput block provides access to all MF624 IRC input configurations and provides option to control reset and gating modes by block input. Signed-off-by: Pavel Pisa --- Makefile | 1 + mf624_SIMULINK.c | 10 ++ mf624_SIMULINK.h | 13 +- sfIRCInput.c | 373 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 396 insertions(+), 1 deletion(-) create mode 100644 sfIRCInput.c diff --git a/Makefile b/Makefile index a2a1402..927a3a3 100644 --- a/Makefile +++ b/Makefile @@ -6,3 +6,4 @@ all: 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 diff --git a/mf624_SIMULINK.c b/mf624_SIMULINK.c index 59242a5..ebd2276 100644 --- a/mf624_SIMULINK.c +++ b/mf624_SIMULINK.c @@ -216,6 +216,16 @@ double ADC_read(mf624_state_t* mfst, adc_channel_t channel) return 10.0 * ((int16_t) (result << 2)) / (double) 0x8000; } +extern uint32_t IRC_mode_change(mf624_state_t* mfst, uint32_t change_mask, uint32_t change_val) +{ + /* This sequence should be protected by mutex to protect changes in multirate systems */ + mfst->IRC_mode = (mfst->IRC_mode & ~change_mask) | (change_val & change_mask); + mf624_write32(mfst->IRC_mode, MFST2REG(mfst, 4, IRCCTRL_reg)); + /*printf("change_mask 0x%08x, change_val 0x%08x\n", change_mask, change_val);*/ + /*printf("IRC mode set to %08lx\n", mfst->IRC_mode);*/ + return mfst->IRC_mode; +} + static int open_device(char* path) { int device_fd; diff --git a/mf624_SIMULINK.h b/mf624_SIMULINK.h index 5c57b1d..c369410 100644 --- a/mf624_SIMULINK.h +++ b/mf624_SIMULINK.h @@ -7,6 +7,14 @@ #include "simstruc.h" +/*masked fields macros*/ +#ifndef __val2mfld +#define __val2mfld(mask,val) (((mask)&~((mask)<<1))*(val)&(mask)) +#endif +#ifndef __mfld2val +#define __mfld2val(mask,val) (((val)&(mask))/((mask)&~((mask)<<1))) +#endif + /* Hardware specific */ /* BAR0 */ #define GPIOC_reg 0x54 @@ -137,7 +145,8 @@ typedef struct mf624_state_t { bar_mapping_t bar4; int status; int ADC_enabled; // Which ADCs are enabled - int DOut; + int DOut; + uint32_t IRC_mode; } mf624_state_t; //extern mf624_state_t mf624_state; @@ -197,6 +206,8 @@ extern int ADC_enable(mf624_state_t* mfst, adc_channel_t channel); extern double ADC_read(mf624_state_t* mfst, adc_channel_t channel); +extern uint32_t IRC_mode_change(mf624_state_t* mfst, uint32_t change_mask, uint32_t change_val); + int mf624_init(SimStruct *S); int mf624_check(SimStruct *S); int mf624_done(); diff --git a/sfIRCInput.c b/sfIRCInput.c new file mode 100644 index 0000000..1cd4367 --- /dev/null +++ b/sfIRCInput.c @@ -0,0 +1,373 @@ +/* + * S-function to support IRC inputs on Humusoft MF624 card + * + * Based on sfuntmpl_basic.c by The MathWorks, Inc. + */ + + +#define S_FUNCTION_NAME sfIRCInput +#define S_FUNCTION_LEVEL 2 + +/* + * The S-function has next parameters + * + * Sample time - sample time value or -1 for inherited + * Counter Mode - + * Counter Gating + * Reset Control + * Digital Filter + */ + +#define PRM_TS(S) (mxGetScalar(ssGetSFcnParam(S, 0))) +#define PRM_CHANNEL(S) (mxGetScalar(ssGetSFcnParam(S, 1))) +#define PRM_COUNTER_MODE(S) (mxGetScalar(ssGetSFcnParam(S, 2))) +#define PRM_COUNTER_GATING(S) (mxGetScalar(ssGetSFcnParam(S, 3))) +#define PRM_RESET_CONTROL(S) (mxGetScalar(ssGetSFcnParam(S, 4))) +#define PRM_DIGITAL_FILTER(S) (mxGetScalar(ssGetSFcnParam(S, 5))) + +#define PRM_COUNT 6 + +#define IWORK_IDX_CHANNEL 0 +#define IWORK_IDX_IRCCTRL 1 +#define IWORK_IDX_USE_GATING_INPUT 2 +#define IWORK_IDX_GATING_VALUE 3 +#define IWORK_IDX_USE_RESET_INPUT 4 +#define IWORK_IDX_RESET_VALUE 5 + +#define IWORK_COUNT 6 + +#define IWORK_CHANNEL(S) (ssGetIWork(S)[IWORK_IDX_CHANNEL]) +#define IWORK_IRCCTRL(S) (ssGetIWork(S)[IWORK_IDX_IRCCTRL]) +#define IWORK_USE_GATING_INPUT(S) (ssGetIWork(S)[IWORK_IDX_USE_GATING_INPUT]) +#define IWORK_GATING_VALUE(S) (ssGetIWork(S)[IWORK_IDX_GATING_VALUE]) +#define IWORK_USE_RESET_INPUT(S) (ssGetIWork(S)[IWORK_IDX_USE_RESET_INPUT]) +#define IWORK_RESET_VALUE(S) (ssGetIWork(S)[IWORK_IDX_RESET_VALUE]) + +/* + * Need to include simstruc.h for the definition of the SimStruct and + * its associated macro definitions. + */ +#include "simstruc.h" +#include "mf624_SIMULINK.h" + +static const int32_T irc_channel2reg[] = { + IRC0_reg, + IRC1_reg, + IRC2_reg, + IRC3_reg +}; + +/* Error handling + * -------------- + * + * You should use the following technique to report errors encountered within + * an S-function: + * + * ssSetErrorStatus(S,"Error encountered due to ..."); + * return; + * + * Note that the 2nd argument to ssSetErrorStatus must be persistent memory. + * It cannot be a local variable. For example the following will cause + * unpredictable errors: + * + * mdlOutputs() + * { + * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"} + * sprintf(msg,"Error due to %s", string); + * ssSetErrorStatus(S,msg); + * return; + * } + * + * See matlabroot/simulink/src/sfuntmpl_doc.c for more details. + */ + +/*====================* + * S-function methods * + *====================*/ + +#define MDL_CHECK_PARAMETERS /* Change to #undef to remove function */ +#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE) + /* Function: mdlCheckParameters ============================================= + * Abstract: + * mdlCheckParameters verifies new parameter settings whenever parameter + * change or are re-evaluated during a simulation. When a simulation is + * running, changes to S-function parameters can occur at any time during + * the simulation loop. + */ +static void mdlCheckParameters(SimStruct *S) +{ + if ((PRM_TS(S) < 0) && (PRM_TS(S) != -1)) + 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"); + 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_COUNTER_GATING(S) < 0) || PRM_COUNTER_GATING(S) > + __mfld2val(IRCCTRL_IRC0COUNT_mask, IRCCTRL_IRC0COUNT_mask)) + if ((PRM_COUNTER_GATING(S) != -1)) + ssSetErrorStatus(S, "Counter Gating out of valid range and -1 for external input not set"); + if ((PRM_RESET_CONTROL(S) < 0) || PRM_RESET_CONTROL(S) > + __mfld2val(IRCCTRL_IRC0RESET_mask, IRCCTRL_IRC0RESET_mask)) + if (PRM_RESET_CONTROL(S) != -1) + ssSetErrorStatus(S, "Reset Control out of valid range and -1 for external input not set"); + 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 /* MDL_CHECK_PARAMETERS */ + + +/* Function: mdlInitializeSizes =============================================== + * Abstract: + * The sizes information is used by Simulink to determine the S-function + * block's characteristics (number of inputs, outputs, states, etc.). + */ +static void mdlInitializeSizes(SimStruct *S) +{ + int_T nInputPorts = 0; + int_T i; + + ssSetNumSFcnParams(S, PRM_COUNT); /* Number of expected parameters */ + if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) { + /* Return if number of expected != number of actual parameters */ + ssSetErrorStatus(S, "6-parameters requited: Ts, Channel, Counter Mode, Counter Gating, Reset Control and Digital Filter"); + return; + } + + #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE) + mdlCheckParameters(S); + if (ssGetErrorStatus(S) != NULL) return; + #endif + + if (PRM_COUNTER_GATING(S) == -1) + nInputPorts++; + if (PRM_RESET_CONTROL(S) == -1) + nInputPorts++; + + ssSetNumContStates(S, 0); + ssSetNumDiscStates(S, 0); + + if (!ssSetNumInputPorts(S, nInputPorts)) return; + for (i = 0; i < nInputPorts; i++) { + ssSetInputPortWidth(S, i, 1); + ssSetInputPortDataType(S, i, SS_UINT8); + ssSetInputPortRequiredContiguous(S, i, true); /*direct input signal access*/ + ssSetInputPortDirectFeedThrough(S, i, 0); + } + /* + * Set direct feedthrough flag (1=yes, 0=no). + * A port has direct feedthrough if the input is used in either + * the mdlOutputs or mdlGetTimeOfNextVarHit functions. + * See matlabroot/simulink/src/sfuntmpl_directfeed.txt. + */ + + if (!ssSetNumOutputPorts(S, 1)) return; + ssSetOutputPortWidth(S, 0, 1); + ssSetOutputPortDataType(S, 0, SS_INT32); + + ssSetNumSampleTimes(S, 1); + ssSetNumRWork(S, 0); + ssSetNumIWork(S, IWORK_COUNT); + ssSetNumPWork(S, 0); + ssSetNumModes(S, 0); + ssSetNumNonsampledZCs(S, 0); + + /* Specify the sim state compliance to be same as a built-in block */ + ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE); + + ssSetOptions(S, 0); +} + + + +/* Function: mdlInitializeSampleTimes ========================================= + * Abstract: + * This function is used to specify the sample time(s) for your + * S-function. You must register the same number of sample times as + * specified in ssSetNumSampleTimes. + */ +static void mdlInitializeSampleTimes(SimStruct *S) +{ + if (PRM_TS(S) == -1) { + ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME); + ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET); + } else { + ssSetSampleTime(S, 0, PRM_TS(S)); + ssSetOffsetTime(S, 0, 0.0); + } +} + + + +#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */ +#if defined(MDL_INITIALIZE_CONDITIONS) + /* Function: mdlInitializeConditions ======================================== + * Abstract: + * In this function, you should initialize the continuous and discrete + * states for your S-function block. The initial states are placed + * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S). + * You can also perform any other initialization activities that your + * S-function may require. Note, this routine will be called at the + * start of simulation and if it is present in an enabled subsystem + * configured to reset states, it will be call when the enabled subsystem + * restarts execution to reset the states. + */ + static void mdlInitializeConditions(SimStruct *S) + { + } +#endif /* MDL_INITIALIZE_CONDITIONS */ + + + +#define MDL_START /* Change to #undef to remove function */ +#if defined(MDL_START) + /* Function: mdlStart ======================================================= + * Abstract: + * This function is called once at start of model execution. If you + * have states that should be initialized once, this is the place + * to do it. + */ +static void mdlStart(SimStruct *S) +{ + int_T shift; + int_T mode; + int_T filter; + int_T mask; + + if (mf624_init(NULL) != 0) + return; + + mask = IRCCTRL_IRC0MODE_mask | IRCCTRL_IRC0COUNT_mask | + IRCCTRL_IRC0RESET_mask | IRCCTRL_IRC0FILTER_mask; + + IWORK_CHANNEL(S) = PRM_CHANNEL(S); + IWORK_GATING_VALUE(S) = PRM_COUNTER_GATING(S); + IWORK_USE_GATING_INPUT(S) = (IWORK_GATING_VALUE(S) == -1)? 1: 0; + IWORK_RESET_VALUE(S) = PRM_RESET_CONTROL(S); + IWORK_USE_RESET_INPUT(S) = (IWORK_RESET_VALUE(S) == -1)? 1: 0; + + mode = PRM_COUNTER_MODE(S); + filter = PRM_DIGITAL_FILTER(S); + + IWORK_IRCCTRL(S) = + __val2mfld(IRCCTRL_IRC0MODE_mask, mode) | + (IWORK_USE_GATING_INPUT(S)? 0 : + __val2mfld(IRCCTRL_IRC0COUNT_mask, IWORK_GATING_VALUE(S))) | + (IWORK_USE_RESET_INPUT(S)? 0 : + __val2mfld(IRCCTRL_IRC0RESET_mask, IWORK_RESET_VALUE(S))) | + __val2mfld(IRCCTRL_IRC0FILTER_mask, filter); + + shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT; + + IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift); +} +#endif /* MDL_START */ + + + +/* Function: mdlOutputs ======================================================= + * Abstract: + * In this function, you compute the outputs of your S-function + * block. + */ +static void mdlOutputs(SimStruct *S, int_T tid) +{ + int32_T *y = ssGetOutputPortSignal(S,0); + + if (mf624_check(S) != 0) + return; + + y[0] = mf624_read32(MFST2REG(mfst, 4, irc_channel2reg[IWORK_CHANNEL(S)])); +} + + + +#define MDL_UPDATE /* Change to #undef to remove function */ +#if defined(MDL_UPDATE) + /* Function: mdlUpdate ====================================================== + * Abstract: + * This function is called once for every major integration time step. + * Discrete states are typically updated here, but this function is useful + * for performing any tasks that should only take place once per + * integration step. + */ +static void mdlUpdate(SimStruct *S, int_T tid) +{ + int_T mask = 0; + int_T shift; + int_T inp_num = 0; + + if (mf624_check(S) != 0) + return; + + if (IWORK_USE_GATING_INPUT(S)) { + const uint8_T *u = (const uint8_T*) ssGetInputPortSignal(S, inp_num); + if (*u != IWORK_GATING_VALUE(S)) { + IWORK_GATING_VALUE(S) = *u; + IWORK_IRCCTRL(S) = (IWORK_IRCCTRL(S) & ~IRCCTRL_IRC0COUNT_mask) | + __val2mfld(IRCCTRL_IRC0COUNT_mask, IWORK_GATING_VALUE(S)); + mask |= IRCCTRL_IRC0COUNT_mask; + } + inp_num++; + } + + if (IWORK_USE_RESET_INPUT(S)) { + const uint8_T *u = (const uint8_T*) ssGetInputPortSignal(S, inp_num); + if (*u != IWORK_RESET_VALUE(S)) { + IWORK_RESET_VALUE(S) = *u; + IWORK_IRCCTRL(S) = (IWORK_IRCCTRL(S) & ~IRCCTRL_IRC0RESET_mask) | + __val2mfld(IRCCTRL_IRC0RESET_mask, IWORK_RESET_VALUE(S)); + mask |= IRCCTRL_IRC0RESET_mask; + } + inp_num++; + } + + if (mask) { + shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT; + IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift); + } +} +#endif /* MDL_UPDATE */ + + + +#undef MDL_DERIVATIVES /* Change to #undef to remove function */ +#if defined(MDL_DERIVATIVES) + /* Function: mdlDerivatives ================================================= + * Abstract: + * In this function, you compute the S-function block's derivatives. + * The derivatives are placed in the derivative vector, ssGetdX(S). + */ + static void mdlDerivatives(SimStruct *S) + { + } +#endif /* MDL_DERIVATIVES */ + + + +/* Function: mdlTerminate ===================================================== + * Abstract: + * In this function, you should perform any actions that are necessary + * at the termination of a simulation. For example, if memory was + * allocated in mdlStart, this is the place to free it. + */ +static void mdlTerminate(SimStruct *S) +{ + mf624_done(); +} + + +/*======================================================* + * See sfuntmpl_doc.c for the optional S-function methods * + *======================================================*/ + +/*=============================* + * Required S-function trailer * + *=============================*/ + +#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */ +#include "simulink.c" /* MEX-file interface mechanism */ +#else +#include "cg_sfun.h" /* Code generation registration function */ +#endif -- 2.39.2