-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# rpi_spi_close | ||
|
||
#' Close the connection to an SPI device | ||
#' | ||
#' @param spiDeviceID an SPI device id as supplied by rpi_spi_open() | ||
#' | ||
#' @return | ||
#' @export | ||
#' | ||
#' @examples | ||
rpi_spi_close <- function(spiDeviceID) { | ||
close(description = spiDeviceID) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# rpi_spi_open | ||
|
||
# good example | ||
# https://raspberry-projects.com/pi/programming-in-c/spi/using-the-spi-interface | ||
|
||
# spi_mode | ||
# https://www.analog.com/en/analog-dialogue/articles/introduction-to-spi-interface.html | ||
|
||
spiIncludes <- c("<fcntl.h>", | ||
"<unistd.h>", | ||
"<sys/ioctl.h>", | ||
"<linux/types.h>", | ||
"<linux/spi/spidev.h>" | ||
) | ||
|
||
#' Open a connection to a Raspberry Pi SPI device | ||
#' | ||
#' @param spiBus 0 or 1. Used as /dev/spidev[spiBus].[spiChan] | ||
#' @param spiChan 0 or 1. Used as /dev/spidev[spiBus].[spiChan] | ||
#' @param spiMode 0-3. Controls clock polarity & clock phase which defines logic low/high and rising/falling edge data sample | ||
#' @param spiBits Bits Per Word. Typically 8 | ||
#' @param spiSpeed Transmission speed. 1000000 = 1MHz | ||
#' | ||
#' @return spiDeviceID which identifies the SPI device | ||
#' @export | ||
#' | ||
#' @examples | ||
rpi_spi_open <- function(spiBus, spiChan, spiMode, spiBits, spiSpeed) { | ||
# we really ought to check these incoming values... | ||
|
||
spiDevString <- paste0("/dev/spidev",spiBus,".",spiChan) | ||
spiDeviceID <- file(description = spiDevString, open = "r+") | ||
|
||
# set the write ioctl mode | ||
stringToEval <- paste0('ioctl(',spiDeviceID,', SPI_IOC_WR_MODE,', spiMode) | ||
evalCpp(stringToEval, | ||
depends = spiIncludes) | ||
|
||
# set the read ioctl mode | ||
stringToEval <- paste0('ioctl(',spiDeviceID,', SPI_IOC_RD_MODE,', spiMode) | ||
evalCpp(stringToEval, | ||
depends = spiIncludes) | ||
|
||
# set the spi write bitsPerWord | ||
stringToEval <- paste0('ioctl(',spiDeviceID,', SPI_IOC_WR_BITS_PER_WORD,', spiBits) | ||
evalCpp(stringToEval, | ||
depends = spiIncludes) | ||
|
||
# set the spi read bitsPerWord | ||
stringToEval <- paste0('ioctl(',spiDeviceID,', SPI_IOC_RD_BITS_PER_WORD,', spiBits) | ||
evalCpp(stringToEval, | ||
depends = spiIncludes) | ||
|
||
# set the spi write speed | ||
stringToEval <- paste0('ioctl(',spiDeviceID,', SPI_IOC_WR_MAX_SPEED_HZ,', spiSpeed) | ||
evalCpp(stringToEval, | ||
depends = spiIncludes) | ||
|
||
# set the spi read speed | ||
stringToEval <- paste0('ioctl(',spiDeviceID,', SPI_IOC_RD_MAX_SPEED_HZ,', spiSpeed) | ||
evalCpp(stringToEval, | ||
depends = spiIncludes) | ||
|
||
return(spiDeviceID) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# rpi_spi_read |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# rpi_spi_readWrite |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# rpi_spi_write |