Minimal example of SPI for Portenta H7

I am looking for a minimal example of SPI usage on the Arduino Portenta H7. In particular reading data from a slave device. The traditional Arduino SPI header does not appear to work, with some digging it appears to be due to the SPI.h not having a wrapper for mbed-os, which I am not too familiar with.

Since any information gained there will likely be of interest to those participating in the discussion here, I'll link to @mateo996's duplicate request on GitHub:

Also posted on Github:
This code has worked for me:

// Minimal SPI example

#include "mbed.h"
#include "Arduino.h"

using namespace mbed;

SPI spi(PC_3, PC_2, PI_1);            // MOSI,MISO,SCK,CS* (* - optional).

void setup() {
  Serial.begin(2000000);              // Initialization of serial communication.
  spi.frequency(1000000);             // Set up your frequency.
  spi.format(16, 3);                  // Messege length (bits), SPI_MODE - check these in your SPI decice's data sheet.


void loop() {
    Serial.println(get_data());       // Retrive the data from SPI device

int get_data(){
  digitalWrite(PI_0, LOW);            // Drive the CS LOW.
  delayMicroseconds(1);               // Wait 1 us to settle.
  int data = spi.write(0);            // Send 0x00 and record reply. 
  digitalWrite(PI_0, HIGH);           // Drive the CS HIGH.
  return data;

There is a number of different libraries referring to the SPI interface, I am still not sure if the above is the correct usage. I have also noticed that is you use .select() and .deselct() methods of the SPI object, they do not drive the CS pin LOW (when the optional parameter in the constructor has been defined). Not sure if this is a bug or my incorrect usage on my part?

@mateo996 the classic Arduino SPI API is fully supported by MbedOS-enabled Arduino boards.

Please, check Minimal example of SPI for Portenta H7 · Issue #131 · arduino/ArduinoCore-mbed · GitHub for a minimal example sketch.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.