🖥️
Communication Protocol
  • i2c protocol in embedded system
  • Working With I2C using c++
  • SPI COMMUNICATION PROTOCOL
  • SPI Implementation with source code in C++
  • UART COMMUNICATION PROTOCOL
  • Linux Serial Ports Using C/C++
  • UART RASPBERRY PI
  • Universal GPIO Access
Powered by GitBook
On this page

SPI Implementation with source code in C++

PreviousSPI COMMUNICATION PROTOCOLNextUART COMMUNICATION PROTOCOL

Last updated 2 years ago

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