Weird library bug with platform io

Thank you very much @Owen_Williams for the platfromio tutorial. I’m relatively new to pio and I didn’tt know the lib_deps = "git_url" trick

I do have a very weird issue when I’m using simpleFoc as a library, as opposed to including it in my source tree and compiling it there.

I use a MagneticSensorSPI which I configure with a custom SPIClass instance.

I also use another SPIClass instance for another purpose.

When I’m compiling as a lib, the frequency of the sck clock is much slower, and the communication with my devices fails. I can measure it with my logic analyser.

It is so weird…

disclaimer, to make things work I had to edit a few things in variant.cpp. I’m using the samd21 with the sparkfun bootloader.

I tried many many things to overcome this but I’m clueless as to why it happens.

I just steped onto this looking for something else:


// For compatibility with sketches designed for AVR @ 16 MHz
// New programs should use SPI.beginTransaction to set the SPI clock
#if F_CPU == 48000000
  #define SPI_CLOCK_DIV2   6
  #define SPI_CLOCK_DIV4   12
  #define SPI_CLOCK_DIV8   24
  #define SPI_CLOCK_DIV16  48
  #define SPI_CLOCK_DIV32  96
  #define SPI_CLOCK_DIV64  192
  #define SPI_CLOCK_DIV128 255
#endif /* F_CPU == 48000000 */
#if F_CPU == 120000000
  #define SPI_CLOCK_DIV2   15
  #define SPI_CLOCK_DIV4   30
  #define SPI_CLOCK_DIV8   60
  #define SPI_CLOCK_DIV16  120
  #define SPI_CLOCK_DIV32  240
  #define SPI_CLOCK_DIV64  480
  #define SPI_CLOCK_DIV128 960
#endif /* F_CPU == 120000000 */

Hi - thanks for pointing this out! I agree we need to look at the SPI code for the MagneticSensor class, I was also having some troubles with it.

I’m pretty much certain it has nothing to do with the MagneticSensor class. I have another instance of SPIClass and it behaves just the same. I was measuring a slowdown of around 3x…

120000000/48000000 = 2.5

so I think F_CPU is missdefined when compiled as a lib. I’ll dig later

Can you not just print out f_cpu?

What board variant are you using?

I’ve not tried messing much with spi clock speed but stumbled on this:

Right now I need to get something else (SimpleFOC-related) done, I haven’t debugged yet since I found the defines, but yeah I will printout the divider and all once I return to it.