Skip to main content

Using BladeRF in Matlab

Normally, I wouldn't write a post such as this. I prefer to talk abou thardware on the blog, but in this case, I didn't see any other tutorials for making this work. The Nuand folks provide a link to pre-release toolbox code, and there is one post on their forums. I'm not sure how much it's going to make a difference, but I'll be using OSX 10.9.2, withXcode 5.1 and Matlab 2013a.

Getting the toolkit compiled

Before we can use the hardware with Matlab, it stands to reason that we need to get the toolkit compiled and installed. To do this, we'll need to setup theXcode command line tools, and Xcodeselect.

Preparing Xcode

The first problem that I ran into was that the Matlab toolkit build system wants to use the OSX 10.7 SDK, which isn't present in Xcode 5.1. The Official solution from Matlab is to edit the mexopts.sh file so that it uses the 10.8 SDK instead.

>> cd(matlabroot)
>> cd bin
>> edit mexopts.sh

Edit the file, replacing all 4 instances of 10.7 with 10.8. Then run 'mex-setup'.

>> mex -setup
>>             Options files control which compiler to use, the compiler and link command
    options, and the runtime libraries to link against.
    much output deleted

Now, we're actually compiling code. But, we're presented with some errors straight-away:

>> run('/Users/wdillon/Desktop/matlab/compile.m')
In file included from bladerf_calibrate_dc.c:2:
In file included from /Applications/MATLAB_R2013a.app/extern/include/matrix.h:294:
/Applications/MATLAB_R2013a.app/extern/include/tmwtypes.h:819:9:
error: unknown type name 'char16_t'
typedef char16_t CHAR16_T;
     ^    bladerf_calibrate_dc.c:4:10: 
fatal error: 'libbladeRF.h' file not found
#include "libbladeRF.h"
     ^    2 errors generated.
            mex: compile of ' "bladerf_calibrate_dc.c"' failed.
            Error using mex (line 206)
    Unable to complete successfully.
            Error in compile (line 1)    mex bladerf_calibrate_dc.c -L. -lbladeRF
            Error in run (line 64)    evalin('caller', [script ';']);

We were tipped-off by the forum that we'd run into this problem. The solution is to copy the libbladerf.h into the directory with the rest of the source files. Presumably, the reason it's not already there is because you don't want to keep another copy of that file that can become inconsistent with the rest of the blade software.

After copying libbladerf.h from the host/libraries/libbladeRF/include folder into the matlab toolkit source folder, we can extinguish that error. For the sake of brevity, let's also handle the type error. In this case, the compiler doesn't know what a 'char16_t' is. If you're a little confused about what a16-bit character is, it's because it has to of with unicode. Also, the bummer is that the tmwtypes file is owned by matlab. So, we can see that bladerf_calibrate_dc.c calls matrix.h, which calls tmwtypes.h. So, before the include for matrix.h, we need to define what char16_t is. So, I just added these two lines to the top of the file:

            #include <stdint.h>    typedef uint16_t char16_t;

The stdint file includes all the definitions for int8_t (8-bit signed integer), uint16_t (16 bit unsigned integer), etc. These are nice because unlike int and 'long int' they don't depend on the architecture of your computer or compiler. With that, we can define char16_t to just be an unsigned16 bit integer. Now, those two errors should be gone, let's try again:

>> run('/Users/wdillon/Desktop/matlab/compile.m')
    bladerf_calibrate_dc.c:24:8: warning: implicit conversion from enumeration type 'bladerf_module' to
    different enumeration type 'bladerf_cal_module' [-Wenum-conversion]
            mod = (bladerf_module)(*m_ptr);
                ~ ^~~~~~~~~~~~~~~~~~~~~~~~
    1 warning generated.
    ld: library not found for -lbladeRF 
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
            mex: link of ' "bladerf_calibrate_dc.mexmaci64″' failed.
            Error using mex (line 206)    Unable to complete successfully.
            Error in compile (line 1)    mex bladerf_calibrate_dc.c -L. -lbladeRF
            Error in run (line 64)    evalin('caller', [script ';']);

Now we've got a brand new error. This time, the linker is trying to find the bladeRF library to link to. Assuming that you've got the bladeRF port installed according to the mac getting starte dguide, it's installed at /opt/local/lib/ so we just need to add that to the library path.In the compile.m file, there are several lines that are define the compile line for each source file. If we rewrite them to include -L/opt/local/lib.

Finally, we're making some progress! Many of the files compiled right away with only a few warnings. When I got to blade_rx, I discovered that these files were written against an older version of the libbladeRF api. At this point, I'll stop giving a play-by-play of the changes, because I think there may be several.

Comments

Comments powered by Disqus