X-Git-Url: http://rtime.felk.cvut.cz/gitweb/mf624-simulink.git/blobdiff_plain/e49158c8824a3d929915cbe722b27534e9c5f778..a2b0c2e846459ae30cf85ac287b19c7b275ab16f:/sfDigitalOutput.c diff --git a/sfDigitalOutput.c b/sfDigitalOutput.c index 445c946..f3fd889 100644 --- a/sfDigitalOutput.c +++ b/sfDigitalOutput.c @@ -1,12 +1,34 @@ /* - * sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function. + * S-function to support digital outputs on Humusoft MF624 card * - * ------------------------------------------------------------------------- - * | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template | - * ------------------------------------------------------------------------- + * Copyright (C) 2013 Michal Kreč + * Copyright (C) 2013 Michal Sojka * - * Copyright 1990-2002 The MathWorks, Inc. - * $Revision: 1.27.4.2 $ + * 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. */ @@ -25,7 +47,10 @@ * its associated macro definitions. */ #include "simstruc.h" + +#ifndef WITHOUT_HW #include "mf624_SIMULINK.h" +#endif /*WITHOUT_HW*/ /* Error handling * -------------- @@ -146,28 +171,11 @@ static void mdlInitializeSampleTimes(SimStruct *S) */ static void mdlStart(SimStruct *S) { - if(mfst==NULL){ - #define BUFF_SMALL 32 - 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; - } - } + #ifndef WITHOUT_HW + if (mf624_init(NULL) != 0) + return; mfst->DOut=0; - + #endif /*WITHOUT_HW*/ } #endif /* MDL_START */ @@ -181,6 +189,11 @@ static void mdlInitializeSampleTimes(SimStruct *S) 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; + if(u[0] > 0.5){ /*Update the "word" holding Output Values*/ mfst->DOut |= (1 << (int)(CHNL_PRM(S)-1)); @@ -191,6 +204,7 @@ static void mdlOutputs(SimStruct *S, int_T tid) } mf624_write16(mfst->DOut, MFST2REG(mfst, 2, DOUT_reg)); + #endif /*WITHOUT_HW*/ } @@ -233,10 +247,9 @@ static void mdlOutputs(SimStruct *S, int_T tid) */ static void mdlTerminate(SimStruct *S) { - if(mfst!=NULL){ - free(mfst); - mfst=NULL; - } + #ifndef WITHOUT_HW + mf624_done(); + #endif /*WITHOUT_HW*/ }