Documentation/Allspark2-Orin NX WikiEnglish · NX
Browse documentation
Additional documentation

SPI C Demo

Spi test Communication C Demo Spi test 1. Spi test.h : Header file that declares functions related to SPI communication. 2. Spi test.c : Source file containing the SPI communica…

2 min read · English documentation

Included Files

Spi_test Communication C Demo Spi_test

  1. Spi_test.h: Header file that declares functions related to SPI communication.
  2. Spi_test.c: Source file containing the SPI communication implementation.
  3. main.c: Main program file containing the program entry point, the main() function. It handles SPI initialization, data transfer, and program control.
  4. Makefile: File used to compile and build the project. It defines the compilation process and the rules for object files and executables.

Source File Spi_test.c:


#include "Spi_test.h"  

#include <stdint.h>

#define SPI_MODE        SPI_MODE_0       // SPI mode: CPOL = 0, CPHA = 0
#define SPI_BITS        8                // Transfer 8 bits of data at a time
#define SPI_SPEED_HZ    50000            // SPI clock frequency: 50 kHz


int open_spi_device(char *device) {
    int spi_fd = open(device, O_RDWR);
    if (spi_fd < 0) {
        perror("Open SPI error");
    }
    return spi_fd;
}


int configure_spi(int spi_fd) {
    int ret;
    int mode = SPI_MODE;
    int bits = SPI_BITS;
    int speed_hz = SPI_SPEED_HZ;
   
    ret = ioctl(spi_fd, SPI_IOC_WR_MODE, &mode);
    if (ret == -1) {
        perror("Failed to set SPI mode");
        return -1;
    }

    
    ret = ioctl(spi_fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    if (ret == -1) {
        perror("Failed to set bits per word");
        return -1;
    }

    
    ret = ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz);
    if (ret == -1) {
        perror("Failed to set clock frequency");
        return -1;
    }

    return 0;
}

int transfer_spi(int spi_fd, u_int8_t *tx_buffer, u_int8_t *rx_buffer, int len) {
    int ret;

    struct spi_ioc_transfer transfer = {
        .tx_buf = (unsigned long)tx_buffer,
        .rx_buf = (unsigned long)rx_buffer,
        .len = len,
        .speed_hz = SPI_SPEED_HZ,
        .bits_per_word = SPI_BITS
    };

    ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &transfer);
    if (ret < 1) {
        perror("Transmission error");
        return -1;
    }

    return 0;
}

int close_spi_device(int spi_fd) {
   int ret = close(spi_fd);
     if (ret < 0) {
        perror("Close SPI error");
        return -1;
    }
}

Function Descriptions

1. open_spi_device(char *device)

Function:

This function opens the specified SPI device and obtains its SPI file descriptor.

Parameter:

device: A string containing the SPI device path (e.g., /dev/spidev0.0).

Return value:

If the SPI device is opened successfully, the function returns the file descriptor (spi_fd).

2. configure_spi(int spi_fd)

Function:

This function configures the SPI mode.

Parameter:

spi_fd: The SPI file descriptor obtained using the open_spi_device() function.

Return value:

Returns 0 if configuration succeeds.

Returns -1 if configuration fails and sets errno to indicate the cause of the error.

3. int transfer_spi(int spi_fd, u_int8_t *tx_buffer, u_int8_t *rx_buffer, int len)

Function:

This function transfers SPI data.

Parameters:

spi_fd: The SPI file descriptor obtained using the open_spi_device() function.

tx_buffer: Array containing the data to send.

rx_buffer: Array that receives the data.

len: Length of tx_buffer.

Return value:

Returns 0 if configuration succeeds.

Returns -1 if configuration fails and sets errno to indicate the cause of the error.

4. int close_spi_device(int spi_fd)

Function:

This function closes the specified SPI device and releases its resources.

Parameter:

spi_fd: The SPI file descriptor obtained using the open_spi_device() function.

Return value:

Returns -1 if configuration fails and sets errno to indicate the cause of the error.

Main Program main.c:

#include <stdio.h>
#include "Spi_test.h"  

void print_usage() {
    printf("Usage: spi_test <device>\n");
    printf("  <device>  : SPI device (e.g., /dev/spidev0.0)\n");
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        print_usage();
        return -1;
    }

    char *device = argv[1];  
    int spi_fd;
    u_int8_t tx_buffer[7] = { 0xAF ,0x48 , 0x66 ,0x01 ,0x0F ,0xBC ,0xFE};   
    u_int8_t rx_buffer[7];               
    int ret,i;

    printf("Starting SPI test...\n");

    
    spi_fd = open_spi_device(device);
    
    configure_spi(spi_fd);
     
    transfer_spi(spi_fd, tx_buffer, rx_buffer, sizeof(tx_buffer));

    printf("Transmitted data:\n");
    for (i=0;i < 7;i++){
        printf("0x%02X ", tx_buffer[i]);
    }

    printf("\n");

    printf("Received data:\n");
    for (i=0;i < 7;i++){
        printf("0x%02X ", rx_buffer[i]);
    }
    printf("\n");

    close_spi_device(spi_fd);

    return 0;
}

Compiling

cd SPI
make

Running

sudo ./Spi_test /dev/spidev0.0

Successful Run

Cleaning

cd SPI
make clean