diff --git a/R/rpi_spi_close.R b/R/rpi_spi_close.R new file mode 100644 index 0000000..ca2d1f1 --- /dev/null +++ b/R/rpi_spi_close.R @@ -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) +} diff --git a/R/rpi_spi_open.R b/R/rpi_spi_open.R new file mode 100644 index 0000000..9c15d4f --- /dev/null +++ b/R/rpi_spi_open.R @@ -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("", + "", + "", + "", + "" +) + +#' 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) +} diff --git a/R/rpi_spi_read.R b/R/rpi_spi_read.R new file mode 100644 index 0000000..dc284d4 --- /dev/null +++ b/R/rpi_spi_read.R @@ -0,0 +1 @@ +# rpi_spi_read diff --git a/R/rpi_spi_readWrite.R b/R/rpi_spi_readWrite.R new file mode 100644 index 0000000..0c074a2 --- /dev/null +++ b/R/rpi_spi_readWrite.R @@ -0,0 +1 @@ +# rpi_spi_readWrite diff --git a/R/rpi_spi_write.R b/R/rpi_spi_write.R new file mode 100644 index 0000000..b0fd645 --- /dev/null +++ b/R/rpi_spi_write.R @@ -0,0 +1 @@ +# rpi_spi_write