Pi IoT In C using Linux drivers

Page 1 of 3

SPI is a very popular way to connect devices to the Raspberry Pi and the good news is that it is well supported by Linux drivers – once you know how.

This content comes from my recently published book:

Raspberry Pi IoT In VS Using linux drivers

By Harry Fairhead

Will buy Amazon.

Content

  1. Choosing a Pi for the IoT

  2. C and Visual Studio Code

  3. Pilots: a first program

  4. The GPIO character pilot
    Clip: GPIO character driver

  5. GPIO using I / O control

  6. GPIO events

  7. The device tree
    Extract: The DHT22

  8. A little electronics

  9. Pulse width modulation
    Extract: The PWM driver

  10. SPI devices
    Extract: The SPI pilot ***NEW!!!

  11. I2C basics

  12. The Linux I2C driver

  13. Advanced I2C

  14. Sensor Drivers – Linux IIO and Hwmon

  15. 1-wire bus

  16. Go further with the drivers

  17. Annex I

In the chapter but not in the book:

  • SPI devices
  • SPI Bus Basics
  • Pi SPI interfaces
  • SPI protocol

SPI driver

Before you can use the SPI bus, you must load its driver. You can do this by adding:

dtparam=spi=on

in the /boot/config.txt file. This loads a driver for SPI0 using two chip select lines. To learn how to activate other SPI channels, see later. For the rest of this section, we’ll be using SPI0.

You can also dynamically activate the driver:

FILE *doCommand(char *cmd)
{
    FILE *fp = popen(cmd, "r");
    if (fp == NULL)
    {
        printf("Failed to run command %s nr", cmd);
        exit(1);
    }
    return fp;
}
void checkSPI0()
{
    FILE *fd = doCommand("sudo  dtparam -l");
    char output[1024];
    int txfound = 0;
    char indicator[] = "spi=on";
    char command[] = "sudo dtparam spi=on";
    while (fgets(output, sizeof(output), fd) != NULL)
    {
        printf("%snr", output);
        fflush(stdout);
        if (strstr(output, indicator) != NULL)
        {
            txfound = 1;
        }
    }
    if (txfound == 0)
    {
        fd = doCommand(command);
        sleep(2);
    }
    pclose(fd);
}

This works by first using the dtparam -l command to list the loaded overlays. If the spinnaker superposition is already loaded, nothing is done. If not, it executes the command:

dtparam spi=on

SPIDev

The interface to the SPI driver is usually called SPIdev and there is a spidev.h header file that provides all the definitions you need to use it. When you load the SPI driver to install SPI Channel not a number of character devices are created in / dev of the general form spidevnot.m or not is the channel number and m is the chip selection row used to control the device.

For example, the basic SPI driver uses channel 0, that is, SPI0 with two chip select lines, so you will find spidev0.0 and spidev0.1 which control the SPI device connected to SPI0 on the chip selection 0 and 1 respectively. By default, spidev0.0 uses pin 26 of GPIO8 and spidev0.1 uses pin 28 of GPIO7 for chip selections.

To work with an SPI device, all you need to do is use ioctl to send requests to the corresponding file. If you want to know more about ioctl, see chapter 4.

There is a range of configuration requests:

  • SPI_IOC_WR_MODE    	
    sets mode
  • SPI_IOC_WR_LSB_FIRST  	
    sets LSB first or last
  • SPI_IOC_WR_BITS_PER_WORD 	
    sets number of bits per word
  • SPI_IOC_WR_MAX_SPEED_HZ 
    sets SPI clock if possible

Note: SPI_IOC_WR_LSB_FIRST is not supported on Pi OS.

There are also queries with WR replaced by RD that read, rather than write, the configuration.


Source link

About Jon Moses

Check Also

Why I can’t wait for improved vertical display support on Chromebooks

Earlier this week, Android Police noticed some upcoming changes in Chrome OS that will allow …

Leave a Reply

Your email address will not be published. Required fields are marked *