100 int spi_init(uint8_t mode, uint32_t speed, uint8_t cs)
112 device =
"/dev/spidev1.0";
115 device =
"/dev/spidev1.1";
118 return pabort(
"invalid chip select value");
122 spi_dev = open(device, O_RDWR);
124 return pabort(
"can't open device");
129 ret = ioctl(spi_dev, SPI_IOC_WR_MODE, &mode);
131 return pabort(
"can't set spi mode");
133 ret = ioctl(spi_dev, SPI_IOC_RD_MODE, &mode);
135 return pabort(
"can't get spi mode");
140 ret = ioctl(spi_dev, SPI_IOC_WR_BITS_PER_WORD, &
bits);
142 return pabort(
"can't set bits per word");
144 ret = ioctl(spi_dev, SPI_IOC_RD_BITS_PER_WORD, &
bits);
146 return pabort(
"can't get bits per word");
151 ret = ioctl(spi_dev, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
153 return pabort(
"can't set max speed hz");
155 ret = ioctl(spi_dev, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
157 return pabort(
"can't get max speed hz");
172 int adc_read(
int spi_dev, uint8_t ch, uint8_t sgl,
short int *data)
175 if( ch<0 || ch>7 )
return FAILURE;
176 uint8_t tx[] = {(ch>>1)|(sgl<<2)|0x08, ch<<7, 0x00,};
182 struct spi_ioc_transfer tr = {
183 .tx_buf = (
unsigned long)tx,
184 .rx_buf = (
unsigned long)rxunion.rx,
186 .delay_usecs =
delay,
191 ret = ioctl(spi_dev, SPI_IOC_MESSAGE(1), &tr);
196 *data=(
short int)(rxunion.rx[2]+( (int8_t)( ((int8_t)(rxunion.rxs[1]&0x1F)) <<3)<<5));
200 int dac_write(
int spi_dev, uint8_t ch, uint8_t _shdn,
unsigned short int data)
204 unsigned short int data16;
207 if( ch<0 || ch>1 )
return FAILURE;
208 if( data>4095 )
return FAILURE;
209 txunion.data16 = 0x6000|(ch<<15)|(_shdn<<12)|(data&0x0FFF);
210 uint8_t tx[] = {txunion.data8[1], txunion.data8[0]};
212 struct spi_ioc_transfer tr = {
213 .tx_buf = (
unsigned long)tx,
214 .rx_buf = (
unsigned long)rx,
216 .delay_usecs =
delay,
221 ret = ioctl(spi_dev, SPI_IOC_MESSAGE(1), &tr);
243 struct spi_ioc_transfer tr = {
244 .tx_buf = (
unsigned long)tx,
245 .rx_buf = (
unsigned long)rx,
247 .delay_usecs =
delay,
251 int ret = ioctl(spi_dev, SPI_IOC_MESSAGE(1), &tr);
int dac_write(int spi_dev, uint8_t ch, uint8_t _shdn, unsigned short int data)
WRITE DATA TO D/A CONVERTER MCP3922.
int spi_trans_bytes(int spi_dev, uint8_t *send, uint8_t *receive, int n)
TRANSFER N BYTES.
#define SUCCESS
Modified from SPI testing utility (using spidev driver) Linux/Documentation/spi/spidev_test.c.
int spi_init(uint8_t mode, uint32_t speed, uint8_t cs)
spi Functions to deal communication by SPI protocol
int adc_read(int spi_dev, uint8_t ch, uint8_t sgl, short int *data)
READ DATA FROM A/D CONVERTER MCP3208.
int pabort(const char *s)
void spi_end(int spi_dev)
TERMINATES SPI DEVICE.