Clear Filters
Clear Filters

Arduino SPI writeRead function unavailable in Simulink

4 views (last 30 days)
I am trying to execute multiple SPI writes in one solver step of a simulink program. So I enclosed all of my SPI write calls in one functions and I initialize the relevant objects before running the simulink code. However, at execution simulink complains about 'writeRead' function being undefined when it tries to execute the transfers. I have included the two program segments that I am using.
% init.m
global arduinodev; global extdac;
arduinodev = arduino('COM4', 'Due', 'Libraries', 'SPI');
extdac = device(arduinodev, 'SPIChipSelectPin', 'D52', 'SPIMode', 1, 'bitorder', 'msbfirst') ;
% MATLAB Function
function dacWrite(extdac, values)
%DACWRITE Writes the 6 values received to the Helmholtz Cage Controller
%DAC
%
l = length(values);
if ( l ~= 6 )
return
end
% code n
coden = 0b10000000 ;
for dac = 1:6
cmd = bitor(coden, dac) ;
val = uint16(values(dac));
if ( val > 0x0fff )
val = 0x0fff ;
end
low = bitshift(uint8(bitand(val,0x00ff)),4);
high = uint8(bitshift(uint16(val),-4,'uint16'));
%fprintf("0x%x 0x%x 0x%x\n", cmd, high, low);
dacout = [cmd, high, low];
writeRead(extdac, dacout);
end %for
dacout = [ 0b11000001 0x00 0x00 ] ; % loadn
writeRead(extdac, dacout);
end
Here is the image of the Simulink Code Block that I am using:
And here is the error I am getting:
Undefined function or variable 'writeRead'.
Function 'MATLAB Function' (#23.590.615), line 21, column 9:
"writeRead(extdac, dacout)"

Accepted Answer

Walter Roberson
Walter Roberson on 9 Jan 2020
Edited: Walter Roberson on 13 Jan 2020
In the case where you are using either no acceleration or else only the first step of acceleration, then you can use coder.extrinsic() to have Simulink talk to the MATLAB engine to run the function.
However, there is currently no way to generate deployable code for the writeRead() call. Notice that https://www.mathworks.com/help/supportpkg/arduinoio/ref/writeread.html does not have any section for "extended capabilities", so there is no code generation available for it.
  1 Comment
Sunip Mukherjee
Sunip Mukherjee on 13 Jan 2020
Thanks! I ended up writing my own Arduino server code that reads 12 bytes over serial and programs the DAC with those, while the Simulink code uses generic serial communication to send the 12 bytes to the Arduino at each cycle.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!