💻
Programming Concept
  • 👨‍🔬About Me
  • 📗C++ STL Concept 2023
    • 📖STL in CPP ( Part I)
    • 📖STL in CPP ( Part II)
    • 📖STL in CPP ( Part III )
    • 📖STL in CPP ( Part IV )
    • 📖STL in CPP ( Part V )
    • 📖STL in CPP ( Part VI )
    • 📖STL in CPP ( Part VII )
  • ✏️Pointers in C / C++
    • 📖Pointers in details
  • 📘Strings in CPP 2023
  • 🕞Thread in C++ 2023
  • 📓Smart Pointer in C++ 2023
  • Basic C++ Topics
    • 💼Type Casting in C++ 2023
  • 💻Advanced C++ Programming
    • 🎓Topics (Syllabus)
    • 🧑‍🎓Inheritance and Polymorphism
    • 🖊️Exception Handling
    • 🏃Runtime Type Information
    • 📔An Overview of Templates
    • 📡Template in C++
  • 💻Embedded Linux
    • 🖥️Embedded Linux OS
      • Build Yocto Project Step By Step
      • How to install apt-get to the Yocto Project image
      • Installing gcc/g++ toolchain in yocto project
      • Installing GDB in yocto project
      • Installing ssh-server-dropbear for embedded linux in yocto project
      • Add Python To A Build
      • Add Boost To A Build
      • Adding Valgrind To Build
      • How To Add A Custom App To A Yocto Build
      • Raspberry Pi
    • 📶Communication Protocols
      • ✏️Working With I2C using c++
      • 📔SPI Implementation with source code in C++
      • 📓Linux Serial Ports Using C/C++
      • 🖱️UART
      • 🖨️Universal GPIO Access
  • 🧑‍💻QT QML
    • 📘QT QML Concept 2023
      • Why Qt Framework and QML
      • Sqlite3 for qtquick application
      • How To Install sqlite3 in windows 11 || Windows 10
      • Signals and Slots in Qt QML
      • Working with Signals and Slots in QML
      • Sorting QML ListModels
      • ListView In QML With Section Delegate
      • Registration of custom enum in QML
      • Registering a QML Type as a Singleton object
      • Using enumerations in QML without C ++
      • Registering a Singleton object to use “Static” methods in QML
      • Connecting JavaScript files to other JavaScript files in a Qt / QML project
      • Positioning in Qt QML with anchors
      • TextInput validators in Qt Qml
      • Qt Qml Abstract Item Model using C++
      • Qt QML List Model
      • How to Register your C++ Class as a QML Type
      • Jabra Speaker Connect Qt Project
      • Qt | One article summarizing QObject
  • ▶️QT Concept
    • ℹ️Icon button
    • 💻Register QML Singletone in CP
Powered by GitBook
On this page

Was this helpful?

  1. Embedded Linux
  2. Communication Protocols

SPI Implementation with source code in C++

PreviousWorking With I2C using c++NextLinux Serial Ports Using C/C++

Last updated 1 year ago

Was this helpful?

To work with the SPI, you also need to add a specific Kernel module: . The Raspberry Pi supports this module, you need to configure it by invoking raspi-config, and then select 3 Interfacing Options and P4 SPI.

To access SPI functions with C/C++, you can use the . Following the , you need to configure the SPI connection, then open the device that you want to connect to, and then use the library method for reading and writing data.

//SOURCE: https://raw.githubusercontent.com/milekium/spidev-lib/master/sample/spidev-testcpp.cc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <spidev_lib++.h>

spi_config_t spi_config;
uint8_t tx_buffer[32];
uint8_t rx_buffer[32];

int  main( void)
{

  SPI *mySPI = NULL;

  spi_config.mode=0;
  spi_config.speed=1000000;
  spi_config.delay=0;
  spi_config.bits_per_word=8;

  mySPI=new SPI("/dev/spidev1.0",&spi_config);

  if (mySPI->begin())
  {
    memset(tx_buffer,0,32);
    memset(rx_buffer,0,32);
    sprintf((char*)tx_buffer,"hello world");
    printf("sending %s, to spidev2.0 in full duplex \n ",(char*)tx_buffer);
    mySPI->xfer(tx_buffer,strlen((char*)tx_buffer),rx_buffer,strlen((char*)tx_buffer));
    printf("rx_buffer=%s\n",(char *)rx_buffer);
    //mySPI->end();
    delete mySPI;
  }
 return 1;
}

💻
📶
📔
Spidev
spidev wrapper library
example code