#define S_FUNCTION_NAME sfReadPWM
#define S_FUNCTION_LEVEL 2
-#define PERIOD_PRM(S) (mxGetScalar(ssGetSFcnParam(S, 0)))
#define CTRX_MODE 2083 //=100000100011, count up, repeat, outpu low,gate by ctrIn
#define CTR4_MODE 35 //100011, count upm, repeat,output low
#define CTR_START ((1 << 0) | (1 << 6) | (1 << 12) | (1 << 24)) //start counters 0,1,2,4
+#define CTR_STOP ((1 << 1) | (1 << 7) | (1 << 13) | (1 << 25)) //stop counters 0,1,2,4
#define CTRCLOCK 50000000
/*
{
/* See sfuntmpl_doc.c for more details on the macros below */
- ssSetNumSFcnParams(S, 1); /* Number of expected parameters */
+ ssSetNumSFcnParams(S, 0); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual parameters */
return;
ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
- if (!ssSetNumInputPorts(S, 1)) return;
- ssSetInputPortWidth(S, 0, 1);
- ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
- /*
- * 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.
- */
- ssSetInputPortDirectFeedThrough(S, 0, 1);
+ if (!ssSetNumInputPorts(S, 0)) return;
+
if (!ssSetNumOutputPorts(S, 3)) return;
ssSetOutputPortWidth(S, 0, 1);
mf624_write32(0,MFST2REG(mfst,4,CTR4));
/*Read values from counters and initialize IWork values with them*/
- ssSetIWorkValue(S,0,(uint)mf624_read32(MFST2REG(mfst,4,CTR0)));
- ssSetIWorkValue(S,1,(uint)mf624_read32(MFST2REG(mfst,4,CTR1)));
- ssSetIWorkValue(S,2,(uint)mf624_read32(MFST2REG(mfst,4,CTR2)));
- ssSetIWorkValue(S,3,(uint)mf624_read32(MFST2REG(mfst,4,CTR4)));
+ ssSetIWorkValue(S,0,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR0)));
+ ssSetIWorkValue(S,1,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR1)));
+ ssSetIWorkValue(S,2,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR2)));
+ ssSetIWorkValue(S,3,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR4)));
/*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));
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
- const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
real_T *y0 = ssGetOutputPortSignal(S,0);
real_T *y1 = ssGetOutputPortSignal(S,1);
real_T *y2 = ssGetOutputPortSignal(S,2);
- uint period;
- uint c0,c1,c2,c4;
+ unsigned int period;
+ unsigned int c0,c1,c2,c4;
c0 = mf624_read32(MFST2REG(mfst,4,CTR0));
c2 = mf624_read32(MFST2REG(mfst,4,CTR2));
c4 = mf624_read32(MFST2REG(mfst,4,CTR4));
- period = (uint)(c4-(uint)ssGetIWorkValue(S,3));
+ period = (unsigned int)(c4-(unsigned int)ssGetIWorkValue(S,3));
- y0[0] = (c0-(uint)ssGetIWorkValue(S,0))/period;
- y1[0] = (c1-(uint)ssGetIWorkValue(S,1))/period;
- y2[0] = (c2-(uint)ssGetIWorkValue(S,2))/period;
+ 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);
static void mdlTerminate(SimStruct *S)
{
if(mfst!=NULL){
+ mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL));
+ free(mfst);
+ mfst=NULL;
+ } else {
+ mfst = malloc(sizeof(mf624_state_t));
+ char buff[BUFF_SMALL];
+ memset(buff, '\0', BUFF_SMALL);
+ mfst->uio_dev = "uio0";
+
+ strncat(buff, "/dev/", 5);
+ strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
+
+ mfst->device_fd = open_device(buff);
+
+ if (mfst->device_fd < 0) {
+ ssSetErrorStatus(S,"open failed");
+ return;
+ }
+
+ if (mmap_regions(mfst) < 0) {
+ ssSetErrorStatus(S,"mmap_regions failed");
+ return;
+ }
+ mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL));
free(mfst);
+ mfst=NULL;
}
}