Skip to content

Commit

Permalink
initial commits
Browse files Browse the repository at this point in the history
  • Loading branch information
mnr committed Nov 23, 2023
1 parent 2440588 commit a325751
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
13 changes: 13 additions & 0 deletions R/rpi_spi_close.R
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)
}
65 changes: 65 additions & 0 deletions R/rpi_spi_open.R
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)
}
1 change: 1 addition & 0 deletions R/rpi_spi_read.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# rpi_spi_read
1 change: 1 addition & 0 deletions R/rpi_spi_readWrite.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# rpi_spi_readWrite
1 change: 1 addition & 0 deletions R/rpi_spi_write.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# rpi_spi_write

0 comments on commit a325751

Please sign in to comment.