X-Git-Url: http://rtime.felk.cvut.cz/gitweb/mf624-simulink.git/blobdiff_plain/8e4367753e49f241b5140519f74c070f58a1ee37..629314ef680aff5d27d5be1615805d585fc866b6:/sfReadPWM.c diff --git a/sfReadPWM.c b/sfReadPWM.c index ac193e4..33399ae 100644 --- a/sfReadPWM.c +++ b/sfReadPWM.c @@ -1,3 +1,35 @@ +/* + * S-function to measure external PWM signal duty cycle on Humusoft MF624 card + * + * Copyright (C) 2013 Michal Kreč + * Copyright (C) 2013 Michal Sojka + * + * Department of Control Engineering + * Faculty of Electrical Engineering + * Czech Technical University in Prague (CTU) + * + * The S-Function for ERT Linux can be distributed in compliance + * with GNU General Public License (GPL) version 2 or later. + * Other licence can negotiated with CTU. + * + * Next exception is granted in addition to GPL. + * Instantiating or linking compiled version of this code + * to produce an application image/executable, does not + * by itself cause the resulting application image/executable + * to be covered by the GNU General Public License. + * This exception does not however invalidate any other reasons + * why the executable file might be covered by the GNU Public License. + * Publication of enhanced or derived S-function files is required + * although. + * + * Linux ERT code is available from + * http://rtime.felk.cvut.cz/gitweb/ert_linux.git + * More CTU Linux target for Simulink components are available at + * http://lintarget.sourceforge.net/ + * + * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish + * required S-function structure. + */ #define S_FUNCTION_NAME sfReadPWM #define S_FUNCTION_LEVEL 2 @@ -134,25 +166,25 @@ static void mdlInitializeSampleTimes(SimStruct *S) return; /*Configuration of desired counter modes*/ - mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR0MODE)); - mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR1MODE)); - mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR2MODE)); - mf624_write32(CTR4_MODE,MFST2REG(mfst,4,CTR4MODE)); + mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR0MODE_reg)); + mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR1MODE_reg)); + mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR2MODE_reg)); + mf624_write32(CTR4_MODE,MFST2REG(mfst,4,CTR4MODE_reg)); /*Set reload values of ctrs 0,1,2,4 to 0 just to be sure*/ - mf624_write32(0,MFST2REG(mfst,4,CTR0)); - mf624_write32(0,MFST2REG(mfst,4,CTR1)); - mf624_write32(0,MFST2REG(mfst,4,CTR2)); - mf624_write32(0,MFST2REG(mfst,4,CTR4)); + mf624_write32(0,MFST2REG(mfst,4,CTR0_reg)); + mf624_write32(0,MFST2REG(mfst,4,CTR1_reg)); + mf624_write32(0,MFST2REG(mfst,4,CTR2_reg)); + mf624_write32(0,MFST2REG(mfst,4,CTR4_reg)); /*Read values from counters and initialize IWork values with them*/ - 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))); + ssSetIWorkValue(S,0,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR0_reg))); + ssSetIWorkValue(S,1,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR1_reg))); + ssSetIWorkValue(S,2,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR2_reg))); + ssSetIWorkValue(S,3,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR4_reg))); /*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)); + mf624_write32(CTR_START,MFST2REG(mfst,4,CTRXCTRL_reg)); } @@ -173,13 +205,13 @@ static void mdlOutputs(SimStruct *S, int_T tid) unsigned int period; unsigned int c0,c1,c2,c4; - if (mf624_init(S) != 0) + if (mf624_check(S) != 0) return; - c0 = mf624_read32(MFST2REG(mfst,4,CTR0)); - c1 = mf624_read32(MFST2REG(mfst,4,CTR1)); - c2 = mf624_read32(MFST2REG(mfst,4,CTR2)); - c4 = mf624_read32(MFST2REG(mfst,4,CTR4)); + 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)); @@ -233,22 +265,11 @@ static void mdlOutputs(SimStruct *S, int_T tid) */ static void mdlTerminate(SimStruct *S) { - if (mf624_init(NULL) != 0) + if (mf624_check(NULL) != 0) return; - /* FIXME: This is ugly! */ - if(mfst!=NULL){ - mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL)); - free(mfst); - mfst=NULL; - } else { - mfst = malloc(sizeof(mf624_state_t)); - if (mf624_init(S) != 0) - return; - mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL)); - free(mfst); - mfst=NULL; - } + mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL_reg)); + mf624_done(); }