SPI with multiple files

13 Dec 2011

I setup SPI in the usual way in main.cpp, and want to use spi.write(val) in another project file, but I cannot find what to do to keep the compiler happy. Help please?

Here is some sample files: Main.cpp

  1. include "mbed.h"
  2. include "myproj.h"

SPI spi(p5, p6, p7); mosi, miso, sclk

int main() {

spi.format(16,0);

spi.frequency(1000000); }

---------- myproj.h

void somefunc(uint16_t val);

---------- otherfile.cpp

  1. include "mbed.h"
  2. include "myproj.h"

void somefunc(uint16_t val) {

spi.write(val);

}

13 Dec 2011

you need a forward declaration in myproj.h or otherfile.cpp like "extern SPI spi;"

14 Dec 2011

Thanks. I was using extern SPI spi(); as the forward declaration

14 Dec 2011

with that you declared a function spi() returning an SPI class