Created
August 11, 2019 00:13
-
-
Save dprilutskii/d5b932d325a51712b6c86d14c1cdef2a to your computer and use it in GitHub Desktop.
Raspberry Pi example for SPI operations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "rpi_spi.h" | |
| #define DEVICE "/dev/spidev0.0" | |
| #define ARRAY_SIZE(a) ( sizeof(a) / sizeof( a[0] ) ) | |
| /* Init SPI */ | |
| uint8_t PI_SPI_Init(spi_t *spi) | |
| { | |
| spi->fd = open(DEVICE, O_RDWR); | |
| if (spi->fd < 0) | |
| { | |
| printf("Can't open device!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| /* | |
| * spi mode | |
| */ | |
| if (ioctl(spi->fd, SPI_IOC_WR_MODE, &spi->mode) == -1) | |
| { | |
| printf("Can't set spi mode!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| if (ioctl(spi->fd, SPI_IOC_RD_MODE, &spi->mode) == -1) | |
| { | |
| printf("Can't get spi mode!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| /* | |
| * bits per word | |
| */ | |
| if (ioctl(spi->fd, SPI_IOC_WR_BITS_PER_WORD, &spi->bits) == -1) | |
| { | |
| printf("Can't set bits per word!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| if (ioctl(spi->fd, SPI_IOC_RD_BITS_PER_WORD, &spi->bits) == -1) | |
| { | |
| printf("Can't get bits per word!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| /* | |
| * max speed hz | |
| */ | |
| if (ioctl(spi->fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi->speed) == -1) | |
| { | |
| printf("Can't set max speed hz!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| if (ioctl(spi->fd, SPI_IOC_RD_MAX_SPEED_HZ, &spi->speed) == -1) | |
| { | |
| printf("Can't get max speed hz!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| printf("\r\n\rSPI mode: %d;\r\n\r", spi->mode); | |
| printf("Bits per word: %d;\r\n\r", spi->bits); | |
| printf("SPI prescaller: %d.\r\n\r", spi->speed); | |
| return (EXIT_SUCCESS); | |
| } | |
| /* Deinit SPI */ | |
| uint8_t PI_SPI_Deinit(spi_t *spi) | |
| { | |
| if ( close(spi->fd) < 0 ) | |
| { | |
| printf("Error close SPI!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| else | |
| { | |
| printf("Close SPI.\r\n\r"); | |
| return (EXIT_SUCCESS); | |
| } | |
| } | |
| /* Write n bytes */ | |
| uint8_t PI_SPI_Transmit(spi_t *spi, uint8_t *tx, int n) | |
| { | |
| struct spi_ioc_transfer transaction = | |
| { | |
| .tx_buf = (unsigned long)tx, | |
| .rx_buf = 0, | |
| .len = n, | |
| .delay_usecs = spi->delay, | |
| .speed_hz = spi->speed, | |
| .bits_per_word = spi->bits, | |
| .cs_change = 0, | |
| }; | |
| if ( ioctl( spi->fd, SPI_IOC_MESSAGE(1), &transaction ) < 1 ) | |
| { | |
| printf("Can't send spi message!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| else | |
| { | |
| #ifdef DEBUG | |
| printf("Transmitted in Hex:\r\n\r\t"); | |
| for (int i = 0; i < n; i++) | |
| { | |
| printf("%.2X ", tx[i]); | |
| } | |
| printf("\r\n\rTransmitted in String:\r\n\r\t"); | |
| for (int i = 0; i < n; i++) | |
| { | |
| printf("%c ", tx[i]); | |
| } | |
| printf("\r\n\r"); | |
| #endif | |
| return (EXIT_SUCCESS); | |
| } | |
| } | |
| /* Read n bytes */ | |
| uint8_t PI_SPI_Receive(spi_t *spi, uint8_t *rx, int n) | |
| { | |
| struct spi_ioc_transfer transaction = | |
| { | |
| .tx_buf = 0, | |
| .rx_buf = (unsigned long)rx, | |
| .len = n, | |
| .delay_usecs = spi->delay, | |
| .speed_hz = spi->speed, | |
| .bits_per_word = spi->bits, | |
| .cs_change = 0, | |
| }; | |
| if ( ioctl( spi->fd, SPI_IOC_MESSAGE(1), &transaction ) < 1 ) | |
| { | |
| printf("Can't recived spi message!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| else | |
| { | |
| #ifdef DEBUG | |
| printf("Received in Hex:\r\n\r\t"); | |
| for (int i = 0; i < n; i++) | |
| { | |
| printf("%.2X ", rx[i]); | |
| } | |
| printf("\r\n\rReceived in String:\r\n\r\t"); | |
| for (int i = 0; i < n; i++) | |
| { | |
| printf("%c ", rx[i]); | |
| } | |
| printf("\r\n\r"); | |
| #endif | |
| return (EXIT_SUCCESS); | |
| } | |
| } | |
| /* Write/Read n bytes */ | |
| uint8_t PI_SPI_TransmitReceive(spi_t *spi, uint8_t *tx, uint8_t *rx, int n) | |
| { | |
| struct spi_ioc_transfer transaction = | |
| { | |
| .tx_buf = (unsigned long)tx, | |
| .rx_buf = (unsigned long)rx, | |
| .len = n, | |
| .delay_usecs = spi->delay, | |
| .speed_hz = spi->speed, | |
| .bits_per_word = spi->bits, | |
| .cs_change = 0, | |
| }; | |
| if ( ioctl( spi->fd, SPI_IOC_MESSAGE(1), &transaction ) < 1 ) | |
| { | |
| printf("Can't recived spi message!\r\n\r"); | |
| return (EXIT_FAILURE); | |
| } | |
| else | |
| { | |
| #ifdef DEBUG | |
| printf("Transmitted in Hex:\r\n\r\t"); | |
| for (int i = 0; i < n; i++) | |
| { | |
| printf("%.2X ", tx[i]); | |
| } | |
| printf("\r\n\r"); | |
| printf("Received in Hex:\r\n\r\t"); | |
| for (int i = 0; i < n; i++) | |
| { | |
| printf("%.2X ", rx[i]); | |
| } | |
| printf("\r\n\r"); | |
| #endif | |
| return (EXIT_SUCCESS); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdint.h> | |
| #include <sys/ioctl.h> | |
| #include <sys/types.h> | |
| #include <sys/stat.h> | |
| #include <fcntl.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include <linux/spi/spidev.h> | |
| #ifndef RPI_SPI_H | |
| #define RPI_SPI_H | |
| #ifdef __cplusplus | |
| extern "C" { | |
| #endif | |
| /* SPI options structure */ | |
| typedef struct | |
| { | |
| int fd; | |
| uint8_t mode; | |
| uint8_t bits; | |
| uint32_t speed; | |
| uint16_t delay; /* uS */ | |
| } spi_t; | |
| enum speeds { | |
| s125_0_MHz = 2, | |
| s62_5_MHz = 4, | |
| s31_2_MHz = 8, | |
| s15_6_MHz = 16, | |
| s7_8_MHz = 32, | |
| s3_9_MHz = 64, | |
| s1953_kHz = 128, | |
| s976_kHz = 256, | |
| s488_kHz = 512, | |
| s244_kHz = 1024, | |
| s122_kHz = 2048, | |
| s61_kHz = 4096, | |
| s30_5_kHz = 8192, | |
| s15_2_kHz = 16384, | |
| s7629_Hz = 32768 | |
| }; | |
| /* Init SPI */ | |
| uint8_t PI_SPI_Init(spi_t *spi); | |
| /* Deinit SPI */ | |
| uint8_t PI_SPI_Deinit(spi_t *spi); | |
| /* Write n bytes */ | |
| uint8_t PI_SPI_Transmit(spi_t *spi, uint8_t *tx, int n); | |
| /* Read n bytes */ | |
| uint8_t PI_SPI_Receive(spi_t *spi, uint8_t *rx, int n); | |
| /* Write/Read n bytes */ | |
| uint8_t PI_SPI_TransmitReceive(spi_t *spi, uint8_t *tx, uint8_t *rx, int n); | |
| #ifdef __cplusplus | |
| } | |
| #endif | |
| #endif /* RPI_SPI_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment