Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
Macros | Functions | Variables
spi_functions.c File Reference
#include "spi_functions.h"
Include dependency graph for spi_functions.c:

Go to the source code of this file.

Macros

#define SUCCESS   1
 Modified from SPI testing utility (using spidev driver) Linux/Documentation/spi/spidev_test.c. More...
 
#define FAILURE   0
 

Functions

int pabort (const char *s)
 
int spi_init (uint8_t mode, uint32_t speed, uint8_t cs)
 spi Functions to deal communication by SPI protocol More...
 
void spi_end (int spi_dev)
 TERMINATES SPI DEVICE. More...
 
int adc_read (int spi_dev, uint8_t ch, uint8_t sgl, short int *data)
 READ DATA FROM A/D CONVERTER MCP3208. More...
 
int dac_write (int spi_dev, uint8_t ch, uint8_t _shdn, unsigned short int data)
 WRITE DATA TO D/A CONVERTER MCP3922. More...
 
int spi_trans_bytes (int spi_dev, uint8_t *send, uint8_t *receive, int n)
 TRANSFER N BYTES. More...
 

Variables

static uint8_t bits = 8
 
static uint16_t delay
 

Macro Definition Documentation

#define FAILURE   0

Definition at line 9 of file spi_functions.c.

Referenced by adc_read(), dac_write(), and spi_trans_bytes().

#define SUCCESS   1

Modified from SPI testing utility (using spidev driver) Linux/Documentation/spi/spidev_test.c.

Definition at line 8 of file spi_functions.c.

Referenced by adc_read(), dac_write(), and spi_trans_bytes().

Function Documentation

int adc_read ( int  spi_dev,
uint8_t  ch,
uint8_t  sgl,
short int *  data 
)

READ DATA FROM A/D CONVERTER MCP3208.

Parameters
chSet Channel (0 .. 7)
sgl(0: to single or 1: to pseudo-differencial)

Definition at line 172 of file spi_functions.c.

References ARRAY_SIZE, delay, FAILURE, and SUCCESS.

Referenced by read_all_data().

173 {
174  int ret;
175  if( ch<0 || ch>7 ) return FAILURE;
176  uint8_t tx[] = {(ch>>1)|(sgl<<2)|0x08, ch<<7, 0x00,};
177  union rxunion{
178  int8_t rxs[ARRAY_SIZE(tx)];
179  uint8_t rx[ARRAY_SIZE(tx)];
180  }rxunion;
181  //rxunion.rx={0, };
182  struct spi_ioc_transfer tr = {
183  .tx_buf = (unsigned long)tx,
184  .rx_buf = (unsigned long)rxunion.rx,
185  .len = ARRAY_SIZE(tx),
186  .delay_usecs = delay,
187  .speed_hz = 0,
188  .bits_per_word = 0,
189  };
190 
191  ret = ioctl(spi_dev, SPI_IOC_MESSAGE(1), &tr);
192  if (ret == 1) return FAILURE;
193 //printf("\nRx = 0x%X 0x%X 0x%X\n",rx[0],rx[1],rx[2]);
194 //printf("\nValor retornado pelo conversor AD: %d\n\n",rx[2]+((rx[1]&0x0F)<<8));
195  //*data=(short int)(rx[2]+((rxs[1]&0x1F)<<8));
196  *data=(short int)(rxunion.rx[2]+( (int8_t)( ((int8_t)(rxunion.rxs[1]&0x1F)) <<3)<<5));
197  return SUCCESS;
198 }
#define FAILURE
Definition: spi_functions.c:9
#define SUCCESS
Modified from SPI testing utility (using spidev driver) Linux/Documentation/spi/spidev_test.c.
Definition: spi_functions.c:8
#define ARRAY_SIZE(a)
Definition: spi_functions.h:29
static uint16_t delay
Definition: spi_functions.c:20

Here is the caller graph for this function:

int dac_write ( int  spi_dev,
uint8_t  ch,
uint8_t  _shdn,
unsigned short int  data 
)

WRITE DATA TO D/A CONVERTER MCP3922.

Definition at line 200 of file spi_functions.c.

References ARRAY_SIZE, delay, FAILURE, and SUCCESS.

Referenced by actuate().

201 {
202  int ret;
203  union txunion{
204  unsigned short int data16;
205  uint8_t data8[2];
206  }txunion;
207  if( ch<0 || ch>1 ) return FAILURE;//pabort("Wrong channel value for DAC");
208  if( data>4095 ) return FAILURE;//pabort("Value exceeded for DAC");
209  txunion.data16 = 0x6000|(ch<<15)|(_shdn<<12)|(data&0x0FFF);
210  uint8_t tx[] = {txunion.data8[1], txunion.data8[0]};
211  uint8_t rx[ARRAY_SIZE(tx)] = {0, };
212  struct spi_ioc_transfer tr = {
213  .tx_buf = (unsigned long)tx,
214  .rx_buf = (unsigned long)rx,
215  .len = ARRAY_SIZE(tx),
216  .delay_usecs = delay,
217  .speed_hz = 0,
218  .bits_per_word = 0,
219  };
220 
221  ret = ioctl(spi_dev, SPI_IOC_MESSAGE(1), &tr);
222  if (ret == 1) return FAILURE;//pabort("can't send spi message");
223 //printf("\nRx = 0x%X 0x%X 0x%X\n",rx[0],rx[1],rx[2]);
224 //printf("\nValor retornado pelo conversor AD: %d\n\n",rx[2]+((rx[1]&0x0F)<<8));
225  return SUCCESS;
226 }
#define FAILURE
Definition: spi_functions.c:9
#define SUCCESS
Modified from SPI testing utility (using spidev driver) Linux/Documentation/spi/spidev_test.c.
Definition: spi_functions.c:8
#define ARRAY_SIZE(a)
Definition: spi_functions.h:29
static uint16_t delay
Definition: spi_functions.c:20

Here is the caller graph for this function:

int pabort ( const char *  s)

Definition at line 11 of file spi_functions.c.

Referenced by spi_init().

12 {
13  perror(s);
14  return -1;
15 }

Here is the caller graph for this function:

void spi_end ( int  spi_dev)

TERMINATES SPI DEVICE.

Definition at line 166 of file spi_functions.c.

167 {
168  close(spi_dev);
169  return;
170 }
int spi_init ( uint8_t  mode,
uint32_t  speed,
uint8_t  cs 
)

spi Functions to deal communication by SPI protocol

INITIALIZE SPI DEVICE

Definition at line 100 of file spi_functions.c.

References bits, and pabort().

Referenced by devices_init().

101 {
102  int ret = 0;
103  int spi_dev;
104  char *device;
105 
106  //parse_opts(argc, argv);
107  /*
108  * chose chipselect:
109  */
110  switch(cs){
111  case 0:
112  device = "/dev/spidev1.0";
113  break;
114  case 1:
115  device = "/dev/spidev1.1";
116  break;
117  default:
118  return pabort("invalid chip select value");
119  }
120 
121 
122  spi_dev = open(device, O_RDWR);
123  if (spi_dev < 0)
124  return pabort("can't open device");
125 //printf("\nspi_dev = %d\n",spi_dev);
126  /*
127  * spi mode
128  */
129  ret = ioctl(spi_dev, SPI_IOC_WR_MODE, &mode);
130  if (ret == -1)
131  return pabort("can't set spi mode");
132 
133  ret = ioctl(spi_dev, SPI_IOC_RD_MODE, &mode);
134  if (ret == -1)
135  return pabort("can't get spi mode");
136 
137  /*
138  * bits per word
139  */
140  ret = ioctl(spi_dev, SPI_IOC_WR_BITS_PER_WORD, &bits);
141  if (ret == -1)
142  return pabort("can't set bits per word");
143 
144  ret = ioctl(spi_dev, SPI_IOC_RD_BITS_PER_WORD, &bits);
145  if (ret == -1)
146  return pabort("can't get bits per word");
147 
148  /*
149  * max speed hz
150  */
151  ret = ioctl(spi_dev, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
152  if (ret == -1)
153  return pabort("can't set max speed hz");
154 
155  ret = ioctl(spi_dev, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
156  if (ret == -1)
157  return pabort("can't get max speed hz");
158 
159  //printf("spi mode: %d\n", mode);
160  //printf("bits per word: %d\n", bits);
161  //printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
162 
163  return spi_dev;
164 }
static uint8_t bits
Definition: spi_functions.c:18
int pabort(const char *s)
Definition: spi_functions.c:11

Here is the call graph for this function:

Here is the caller graph for this function:

int spi_trans_bytes ( int  spi_dev,
uint8_t *  send,
uint8_t *  receive,
int  n 
)

TRANSFER N BYTES.

Fix this to show all data

Definition at line 228 of file spi_functions.c.

References ARRAY_SIZE, delay, FAILURE, and SUCCESS.

Referenced by enc_read_pos(), enc_wait_for_ack(), and enc_zero_set().

228  {
229 
230  //< define send data
231  uint8_t *tx;
232  tx = send;
233  uint8_t rx[ARRAY_SIZE(tx)] = {0, };
234  //rx[0] = 0;
235  int i;
236 
237  if(ARRAY_SIZE(tx)<n)
238  return FAILURE;
239 
240  /*
241  * Transmitting data (full duplex):
242  */
243  struct spi_ioc_transfer tr = {
244  .tx_buf = (unsigned long)tx,
245  .rx_buf = (unsigned long)rx,
246  .len = ARRAY_SIZE(tx),
247  .delay_usecs = delay,
248  .speed_hz = 0,
249  .bits_per_word = 0,
250  };
251  int ret = ioctl(spi_dev, SPI_IOC_MESSAGE(1), &tr);
252 
253  //Check transmittion:
254  if(ret == 1) return FAILURE;//pabort("can't send spi message");
256  for( i=0; i<n; i++)
257  receive[i] = rx[i];//get the received data
258 
259  return SUCCESS;
260 }
#define FAILURE
Definition: spi_functions.c:9
#define SUCCESS
Modified from SPI testing utility (using spidev driver) Linux/Documentation/spi/spidev_test.c.
Definition: spi_functions.c:8
#define ARRAY_SIZE(a)
Definition: spi_functions.h:29
static uint16_t delay
Definition: spi_functions.c:20

Here is the caller graph for this function:

Variable Documentation

uint8_t bits = 8
static

Definition at line 18 of file spi_functions.c.

Referenced by conv_byte_hex_bin(), and spi_init().

uint16_t delay
static

Definition at line 20 of file spi_functions.c.

Referenced by adc_read(), dac_write(), and spi_trans_bytes().