spi: implemented write functions
Still basic, more work needed Signed-off-by: Thomas Ingleby <thomas.c.ingleby@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
0c7939a556
commit
9186b79967
@@ -23,13 +23,39 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <linux/spi/spidev.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include "spi.h"
|
#include "spi.h"
|
||||||
|
|
||||||
|
#define MAX_SIZE 64
|
||||||
|
#define SPI_MAX_LENGTH 4096
|
||||||
|
|
||||||
maa_spi_context*
|
maa_spi_context*
|
||||||
maa_spi_init()
|
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
|
maa_result_t
|
||||||
@@ -44,10 +70,47 @@ maa_spi_frequency(maa_spi_context* spi, int hz)
|
|||||||
return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
uint8_t
|
||||||
maa_spi_write(maa_spi_context* spi, unsigned int data)
|
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
|
maa_result_t
|
||||||
|
|||||||
Reference in New Issue
Block a user