From 9186b79967fa37506684f60a90b1c7790fc77245 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 16 May 2014 11:50:34 +0100 Subject: [PATCH] spi: implemented write functions Still basic, more work needed Signed-off-by: Thomas Ingleby --- src/spi/spi.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/src/spi/spi.c b/src/spi/spi.c index bbf4df0..847c4d0 100644 --- a/src/spi/spi.c +++ b/src/spi/spi.c @@ -23,13 +23,39 @@ */ #include +#include +#include +#include +#include +#include #include "spi.h" +#define MAX_SIZE 64 +#define SPI_MAX_LENGTH 4096 + maa_spi_context* maa_spi_init() { - return NULL; + double bus = maa_check_spi(); + if(bus < 0) { + fprintf(stderr, "Failed. SPI platform Error\n"); + return NULL; + } + maa_spi_context* dev = (maa_spi_context*) malloc(sizeof(maa_spi_context)); + memset(dev, 0, sizeof(maa_spi_context)); + + char path[MAX_SIZE]; + sprintf(path, "/dev/spidev%.1f", bus); + + dev->spifd = open(path, O_RDWR); + if (dev->spifd < 0) { + fprintf(stderr, "Failed opening SPI Device. bus:%s\n", path); + free(dev); + return NULL; + } + + return dev; } maa_result_t @@ -44,10 +70,47 @@ maa_spi_frequency(maa_spi_context* spi, int hz) return MAA_ERROR_FEATURE_NOT_IMPLEMENTED; } -unsigned int -maa_spi_write(maa_spi_context* spi, unsigned int data) +uint8_t +maa_spi_write(maa_spi_context* spi, uint8_t data) { - return 0; + struct spi_ioc_transfer msg; + memset(&msg, 0, sizeof(msg)); + + uint16_t length = 1; + + uint8_t recv = 0; + msg.tx_buf = (unsigned long) &data; + msg.rx_buf = (unsigned long) &recv; + msg.speed_hz = 100000; + msg.bits_per_word = 8; + msg.delay_usecs = 0; + msg.len = length; + if(ioctl(spi->spifd, SPI_IOC_MESSAGE(1), &msg) < 0) { + fprintf(stderr, "Failed to perform spi transfer\n"); + return -1; + } + return recv; +} + +uint8_t* +maa_spi_write_buf(maa_spi_context* spi, uint8_t* data, int length) +{ + struct spi_ioc_transfer msg; + memset(&msg, 0, sizeof(msg)); + + uint8_t* recv = malloc(sizeof(uint8_t) * length); + + msg.tx_buf = (unsigned long) data; + msg.rx_buf = (unsigned long) recv; + msg.speed_hz = 100000; + msg.bits_per_word = 8; + msg.delay_usecs = 0; + msg.len = length; + if(ioctl(spi->spifd, SPI_IOC_MESSAGE(1), &msg) < 0) { + fprintf(stderr, "Failed to perform spi transfer\n"); + return NULL; + } + return recv; } maa_result_t