s-Function Builder Help
12 views (last 30 days)
Show older comments
Hello all, I'm trying to create the S-function for the MCP4725 DAC. I've got it working on the Arduino IDE and the code looks like this (extracted from the Adafruit drivers to eliminate the need to copy the Adafruit Includes in the working directory):
****************************************
#include "math.h" // include the Math Library
#define DAC_ADD (0x60)
#define WriteOnly (0x40) //command to update AO only
#define WriteAndStore (0x60) //command to update AO and save AO setting to EEPROM
int a;
void setup(void) {
Serial.begin(9600);
Serial.println("Hello!");
Serial.println("DAC tester");
Serial.flush();
Wire.begin(); //join i2c bus as master
}
void loop () {
Serial.println("Enter value for leg 'a' from 0 - 4095, Press ENTER"); //asks for user input at serial monitor
while (Serial.available() == 0) ; // Wait here until input buffer has a character
{
//user input
a = Serial.parseInt(); // new command in 1.0 forward
Serial.print("a = "); Serial.println(a, DEC); //echos user input at the serial monitor
int twbrback = TWBR; //i2c specific and don't understand it
TWBR = 12; // 400 khz //i2c specific and it sets the comm freq.
Wire.beginTransmission(DAC_ADD); //tell the i2c device that data is comming to it
Wire.write(WriteOnly); // first byte in message for MPC4725
Wire.write(a / 16); //Second byte
Wire.write((a % 16) << 4); //third byte
Wire.endTransmission(); //Tell device all instruction shave been trasnmitted
TWBR = twbrback; //i2c specific
}
}
****************************************
NOTE that comments have been wrapped to the next line here. The code works and I can update the Analog Output of the board by entering a count value (0 - 4095) at the serial monitor.
I've tried to create the s function with s function builder but keep getting the following error:
****************************************
Error MCP4725_Output_v1.c: 82 missing parameter type
Error MCP4725_Output_v1.c: 196 type error in argument 2 to `MCP4725_Output_v1_Update_wrapper'; found `pointer to double' expected `int'
Error MCP4725_Output_v1.c: 196 insufficient number of arguments to `MCP4725_Output_v1_Update_wrapper' 3 errors, 0 warnings
C:\PROGRA~2\MATLAB\R2013A\BIN\MEX.PL: Error: Compile of 'MCP4725_Output_v1.c' failed.
****************************************
I put the following in the different s-function tabs
Discrete Update: if (xD[0]!=1) {
# ifndef MATLAB_MEX_FILE
Wire.begin(); // start i2c bus
# endif
// initialization done
xD[0]=1;
}
Outputs: // wait until after initialization is done if (xD[0]==1) {
# ifndef MATLAB_MEX_FILE
int twbrback = TWBR;
TWBR = 0x0c; // 400 khz
Wire.beginTransmission(DAC_ADD);
Wire.write(WriteOnly);
Wire.write(DAC_in[0] / 16);
Wire.write((DAC_in[0 % 16) << 4);
Wire.endTransmission();
TWBR = twbrback;
# endif
}
Libraries (include...):
#ifndef MATLAB_MEX_FILE
// Include the following i2c libraries // pulled from Arduino ver 1.0
// DAC Address Defines
#define DAC_ADD 0x60
#define WriteOnly 0x40
#define WriteAndStore 0x60
- endif
Data Properties:
INPUT: DAC_in (1-D, 1, , real, off)
OUTPUT: none
PARAMETERS: none
DATA TYPE ATTR...: In_1: DAC_in (uint16, 12, checked, 3, 2^-3, 0)
For the data type I've tried all the settings available and none worked
I've also copied all the libraries to the working directory and I know this is working because I was able to run the Adxl345 I2c Driver for Arduino Mega that this thread inspired....
What am I doing wrong???
Thanks for the help...
2 Comments
Jordan Monthei
on 13 May 2013
Your errors reference lines 82 and 196, what is the code on each of those lines?
Accepted Answer
Guillermo
on 15 May 2013
2 Comments
Barza Nisar
on 4 Aug 2015
Edited: Barza Nisar
on 4 Aug 2015
Hey Guillermo, I am trying to run (and/or deploy) your sFunction on Arduino Mega 2560. I have the arduino support package and also included the Wire.h,Wire.cpp,twi.h and twi.c files in the same folder as the s function. I also changed the wrapper file extension to .cpp and added extern "C" before void wrapper functions. However, I receive the following error to which I have not yet found any solution: The call to realtime_make_rtw_hook, during the after_make hook generated the following error:
The build failed with the following message: "D:/MATLABSupportPack/arduino-1.0.5/hardware/tools/avr/bin/avr-gcc" -I"D:/MATLABSupportPack/arduino/include" -I"D:/MATLABSupportPack/arduino/blocks/sfcn/include" -I"D:/DACsFuncSimulink/MPC4725_toggle_rtt" -I"D:/DACsFuncSimulink" -I"D:/NewMatlab/extern/include" -I"D:/NewMatlab/simulink/include" -I"D:/NewMatlab/rtw/c/src" -I"D:/NewMatlab/rtw/c/src/ext_mode/common" -I"D:/NewMatlab/rtw/c/ert" -I"D:/NewMatlab/toolbox/coder/rtiostream/src/utils" -I"D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino" -I"D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/variants/mega" -I"D:/MATLABSupportPack/arduino-1.0.5/libraries/Servo" -I"D:/NewMatlab/rtw/c/src/ext_mode/serial" -I"D:/MATLABSupportPack/arduinomega2560/src" -mmcu=atmega2560 -ffunction-sections -fdata-sections -std=gnu99 -Wall -Wstrict-prototypes -g -Os -D"MODEL=MPC4725_toggle" -D"NUMST=3" -D"NCSTATES=0" -D"HAVESTDIO=" -D"ON_TARGET_WAIT_FOR_START=1" -D"ONESTEPFCN=0" -D"EXT_MODE=1" -D"TERMFCN=1" -D"MAT_FILE=0" -D"MULTI_INSTANCE_CODE=0" -D"INTEGER_CODE=0" -D"MT=0" -D"CLASSIC_INTERFACE=0" -D"ALLOCATIONFCN=0" -D"TID01EQ=0" -D"F_CPU=16000000" -D"ARDUINO=105" -D"_RUNONTARGETHARDWARE_BUILD_=" -D"_ROTH_MEGA2560_=" -D"_RTT_NUMSERVOS_=0" -D"EXIT_FAILURE=1" -D"EXTMODE_DISABLEPRINTF=" -D"EXTMODE_DISABLETESTING=" -D"EXTMODE_DISABLE_ARGS_PROCESSING=1" -c -x none ./MPC4725_S_Function_wrapper.cpp ./MPC4725_toggle.c ./MPC4725_toggle_data.c ./ert_main.c ./HardwareSerial.cpp ./IPAddress.cpp ./Print.cpp ./WInterrupts.c ./WMath.cpp ./WString.cpp ./new.cpp ./wiring.c ./wiring_analog.c ./wiring_digital.c ./io_wrappers.cpp ./rtiostream_serial.cpp ./ext_svr.c ./ext_work.c ./updown.c ./ext_serial_pkt.c ./ext_svr_serial_transport.c ./rtiostream_serial_interface.c ./rtiostream_utils.c
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
In file included from ./MPC4725_S_Function_wrapper.cpp:18:
D:/DACsFuncSimulink/twi.h: In member function 'uint8_t TwoWire::requestFrom(uint8_t, uint8_t, uint8_t)':
D:/DACsFuncSimulink/twi.h:47: error: too many arguments to function 'uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t)'
D:/DACsFuncSimulink/Wire.cpp:85: error: at this point in file
D:/DACsFuncSimulink/twi.h: In member function 'uint8_t TwoWire::endTransmission(uint8_t)':
D:/DACsFuncSimulink/twi.h:48: error: too many arguments to function 'uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t)'
D:/DACsFuncSimulink/Wire.cpp:140: error: at this point in file
D:/DACsFuncSimulink/Wire.cpp: In member function 'virtual size_t TwoWire::write(const uint8_t*, size_t)':
D:/DACsFuncSimulink/Wire.cpp:195: error: invalid conversion from 'const uint8_t*' to 'uint8_t*'
D:/DACsFuncSimulink/Wire.cpp:195: error: initializing argument 1 of 'uint8_t twi_transmit(uint8_t*, uint8_t)'
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./MPC4725_toggle.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "true" redefined
In file included from ./MPC4725_toggle.h:24,
from ./MPC4725_toggle.c:17:
./rtwtypes.h:27:1: warning: this is the location of the previous definition
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./MPC4725_toggle.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:26:1: warning: "false" redefined
In file included from ./MPC4725_toggle.h:24,
from ./MPC4725_toggle.c:17:
./rtwtypes.h:23:1: warning: this is the location of the previous definition
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./MPC4725_toggle_data.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "true" redefined
In file included from ./MPC4725_toggle.h:24,
from ./MPC4725_toggle_data.c:17:
./rtwtypes.h:27:1: warning: this is the location of the previous definition
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./MPC4725_toggle_data.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:26:1: warning: "false" redefined
In file included from ./MPC4725_toggle.h:24,
from ./MPC4725_toggle_data.c:17:
./rtwtypes.h:23:1: warning: this is the location of the previous definition
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./ert_main.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "true" redefined
In file included from ./MPC4725_toggle.h:24,
from ./ert_main.c:17:
./rtwtypes.h:27:1: warning: this is the location of the previous definition
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./ert_main.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:26:1: warning: "false" redefined
In file included from ./MPC4725_toggle.h:24,
from ./ert_main.c:17:
./rtwtypes.h:23:1: warning: this is the location of the previous definition
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
./HardwareSerial.cpp: In function 'void store_char(unsigned char, ring_buffer*)':
./HardwareSerial.cpp:98: warning: comparison between signed and unsigned integer expressions
./HardwareSerial.cpp: In function 'void __vector_25()':
./HardwareSerial.cpp:127: warning: unused variable 'c'
./HardwareSerial.cpp: In function 'void __vector_36()':
./HardwareSerial.cpp:153: warning: unused variable 'c'
./HardwareSerial.cpp: In function 'void __vector_51()':
./HardwareSerial.cpp:168: warning: unused variable 'c'
./HardwareSerial.cpp: In function 'void __vector_54()':
./HardwareSerial.cpp:183: warning: unused variable 'c'
./HardwareSerial.cpp: In member function 'void HardwareSerial::begin(long unsigned int, byte)':
./HardwareSerial.cpp:368: warning: unused variable 'current_config'
./HardwareSerial.cpp: In member function 'virtual size_t HardwareSerial::write(uint8_t)':
./HardwareSerial.cpp:467: warning: comparison between signed and unsigned integer expressions
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
./Print.cpp: In member function 'size_t Print::print(const __FlashStringHelper*)':
./Print.cpp:44: warning: '__progmem__' attribute ignored
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
./io_wrappers.cpp: In function 'void Serial_read(int, int, uint8_t*, int16_t*)':
./io_wrappers.cpp:59: warning: 'libFcnOutput' may be used uninitialized in this function
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
./rtiostream_serial.cpp: In function 'int rtIOStreamOpen(int, void**)':
./rtiostream_serial.cpp:28: warning: unused variable 'init_complete'
./ext_work.c:248: warning: 'displayUsage' defined but not used
make: *** [MPC4725_S_Function_wrapper.o] Error 1
If you know where I am going wrong, please correct me. Will be very grateful to you. Thank you!
More Answers (2)
Mirko
on 25 May 2014
Hello, I'm trying to make a connection between raspberry I2C (Master) and Arduino (slave). I edited this S-Function, I can read data from the slave, but I do not know how to write to the slave.
LIBRARIAS
# ifndef MATLAB_MEX_FILE
// must run command:
// gpio load i2c
// on RPI to start I2C
// path to files on the local RPi:
#include </home/pi/wiringPi/wiringPi/wiringPiI2C.h>
#include </home/pi/wiringPi/wiringPi/wiringPiI2C.c>
#include </home/pi/wiringPi/wiringPi/wiringPi.c>
#include </home/pi/wiringPi/wiringPi/piHiPri.c>
//----> MPU6050 Info: <-----
// setup defines: Register definitions
#define Ardunio_RG 0x6B
// Device identification:
//#define MPU6050_RA_WHO_AM_I 0x75
// Arduino Read registers:
#define Arduino_XOUT_H 0x43
#define Arduino_XOUT_L 0x44
// global variables for address, high and low bits for data
int fd, gxh, gxl;
#endif
OUTPUT
if(xD[0] == 1)
{
#ifndef MATLAB_MEX_FILE
// Read high byte, low byte, then combine:
gxh = wiringPiI2CReadReg8(fd,Arduino_XOUT_H) ;
gxl = wiringPiI2CReadReg8(fd,Arduino_XOUT_L) ;
y0[0] = ((gxh<<8)|gxl);;
#endif
}
DRISCRETE UPDATE
if(xD[0] != 1){
# ifndef MATLAB_MEX_FILE
// Controllo se Arduino è connesso
fd = wiringPiI2CSetup(0x36); // 0x36 indirizzo Arduino Uno
wiringPiI2CWriteReg8(fd, Ardunio_RG , 0b00000010);
#endif
//done with initialization
xD[0] = 1;
}
I would like to create an S-Function that performs the writing on the slave,have you something to suggest? Thanks
0 Comments
mohammad morad
on 13 Apr 2016
Edited: mohammad morad
on 13 Apr 2016
Dear all I am trying to run Arduino code in S-Function Builder . I have the Arduino support package and also included the library files in the same folder as the s function.but when I build my s-function I receive the following error: test2_wrapper.c C:\Users\Novin Pendar\Desktop\sfunction\sht10\SHT1x 2\test2_wrapper.c(15) : error C2143: syntax error : missing '{' before 'constant' C:\Users\Novin Pendar\Desktop\sfunction\sht10\SHT1x 2\test2_wrapper.c(15) : error C2059: syntax error : '<Unknown>' .......................
I attach my Arduino and Simulink file in this question
please help me
Your answers can be a great help to me
thanks a lot!
See Also
Categories
Find more on Applications in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!