Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
spi_functions.c
Go to the documentation of this file.
1 
6 #include "spi_functions.h"
7 
8 #define SUCCESS 1
9 #define FAILURE 0
10 
11 int pabort(const char *s)
12 {
13  perror(s);
14  return -1;
15 }
16 
17 //static uint8_t mode = 0;
18 static uint8_t bits = 8;
19 //static uint32_t speed = 46875;
20 static uint16_t delay;
21 
22 /*
23 void print_usage(const char *prog)
24 {
25  printf("Usage: %s [-DsbdlHOLC3]\n", prog);
26  puts(" -D --device device to use (default /dev/spidev1.1)\n"
27  " -s --speed max speed (Hz)\n"
28  " -d --delay delay (usec)\n"
29  " -b --bpw bits per word \n"
30  " -l --loop loopback\n"
31  " -H --cpha clock phase\n"
32  " -O --cpol clock polarity\n"
33  " -L --lsb least significant bit first\n"
34  " -C --cs-high chip select active high\n"
35  " -3 --3wire SI/SO signals shared\n");
36  exit(1);
37 }
38 
39 void parse_opts(int argc, char *argv[])
40 {
41  while (1) {
42  static const struct option lopts[] = {
43  { "device", 1, 0, 'D' },
44  { "speed", 1, 0, 's' },
45  { "delay", 1, 0, 'd' },
46  { "bpw", 1, 0, 'b' },
47  { "loop", 0, 0, 'l' },
48  { "cpha", 0, 0, 'H' },
49  { "cpol", 0, 0, 'O' },
50  { "lsb", 0, 0, 'L' },
51  { "cs-high", 0, 0, 'C' },
52  { "3wire", 0, 0, '3' },
53  { NULL, 0, 0, 0 },
54  };
55  int c;
56 
57  c = getopt_long(argc, argv, "D:s:d:b:lHOLC3", lopts, NULL);
58 
59  if (c == -1)
60  break;
61 
62  switch (c) {
63  case 'D':
64  device = optarg;
65  break;
66  case 's':
67  speed = atoi(optarg);
68  break;
69  case 'd':
70  delay = atoi(optarg);
71  break;
72  case 'b':
73  bits = atoi(optarg);
74  break;
75  case 'l':
76  mode |= SPI_LOOP;
77  break;
78  case 'H':
79  mode |= SPI_CPHA;
80  break;
81  case 'O':
82  mode |= SPI_CPOL;
83  break;
84  case 'L':
85  mode |= SPI_LSB_FIRST;
86  break;
87  case 'C':
88  mode |= SPI_CS_HIGH;
89  break;
90  case '3':
91  mode |= SPI_3WIRE;
92  break;
93  default:
94  print_usage(argv[0]);
95  break;
96  }
97  }
98 }
99 */
100 int spi_init(uint8_t mode, uint32_t speed, uint8_t cs/*int argc, char *argv[]*/)
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 }
165 
166 void spi_end(int spi_dev)
167 {
168  close(spi_dev);
169  return;
170 }
171 
172 int adc_read(int spi_dev, uint8_t ch, uint8_t sgl, short int *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 }
199 
200 int dac_write(int spi_dev, uint8_t ch, uint8_t _shdn, unsigned short int data)
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 }
227 
228 int spi_trans_bytes(int spi_dev,uint8_t *send, uint8_t *receive,int n){
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 }
int dac_write(int spi_dev, uint8_t ch, uint8_t _shdn, unsigned short int data)
WRITE DATA TO D/A CONVERTER MCP3922.
#define FAILURE
Definition: spi_functions.c:9
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.
Definition: spi_functions.c:8
#define ARRAY_SIZE(a)
Definition: spi_functions.h:29
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.
static uint16_t delay
Definition: spi_functions.c:20
static uint8_t bits
Definition: spi_functions.c:18
int pabort(const char *s)
Definition: spi_functions.c:11
void spi_end(int spi_dev)
TERMINATES SPI DEVICE.