Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
adc_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Freek van Tienen <freek.v.tienen@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
29 #include "mcu_periph/adc.h"
30 
31 #include BOARD_CONFIG
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <poll.h>
39 
40 
41 /******************************/
42 /*** INTERNAL VARIABLES ***/
43 /******************************/
44 
45 /* ADC0 */
46 #if USE_ADC0
47 static uint8_t adc0_channels[] = {ADC0_CHANNELS};
48 struct adc_t adc0 = {
49  .dev_id = ADC0_ID,
50  .channels = adc0_channels,
51  .channels_cnt = ADC0_CHANNELS_CNT,
52  .buf_length = ADC0_BUF_LENGTH
53 };
54 #endif
55 
56 /* ADC1 */
57 #if USE_ADC1
58 static uint8_t adc1_channels[] = {ADC1_CHANNELS};
59 struct adc_t adc1 = {
60  .dev_id = ADC1_ID,
61  .channels = adc1_channels,
62  .channels_cnt = ADC1_CHANNELS_CNT,
63  .buf_length = ADC1_BUF_LENGTH
64 };
65 #endif
66 
67 /* Private functions */
68 static inline void adc_dev_init(struct adc_t *adc);
69 static void write_sysfs_int(uint8_t dev_id, char *filename, int val);
70 
71 /***************************************/
72 /*** PUBLIC FUNCTION DEFINITIONS ***/
73 /***************************************/
74 
78 void adc_init(void)
79 {
80 #if USE_ADC0
81  adc_dev_init(&adc0);
82 #endif
83 #if USE_ADC1
84  adc_dev_init(&adc1);
85 #endif
86 }
87 
91 void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
92 {
93 
94 }
95 
101 void adc_enable(struct adc_t *adc, uint8_t value)
102 {
103  /* Write 1 or 0 to enable/disable the ADC */
104  write_sysfs_int(adc->dev_id, "buffer/enable", value);
105 }
106 
113 int adc_read(struct adc_t *adc, uint16_t *buf, uint16_t size)
114 {
115  /* Allocate dev_id + name */
116  char *temp;
117  if(asprintf(&temp, "/dev/iio:device%d", adc->dev_id) < 0) {
118  return -1;
119  }
120 
121  /* Open the file */
122  int fd = open(temp, O_RDONLY | O_NONBLOCK);
123  free(temp);
124 
125  if(fd < 0) {
126  return -2;
127  }
128 
129  struct pollfd pfd;
130  pfd.fd = fd;
131  pfd.events = POLLIN;
132  poll(&pfd, 1, -1);
133 
134  /* Read the file */
135  int ret = read(fd, buf, size);
136  close(fd);
137  return ret;
138 }
139 
140 /****************************************/
141 /*** PRIVATE FUNCTION DEFINITIONS ***/
142 /****************************************/
143 
148 static inline void adc_dev_init(struct adc_t *adc)
149 {
150  char filename[32];
151  uint8_t i;
152 
153  /* Enable all linked channels */
154  for(i = 0; i < adc->channels_cnt; i++) {
155  sprintf(filename, "scan_elemens/in_voltage%d_en", adc->channels[i]);
156  write_sysfs_int(adc->dev_id, filename, 1);
157  }
158 
159  /* Set the buffer length */
160  write_sysfs_int(adc->dev_id, "buffer/length", adc->buf_length);
161 }
162 
169 static void write_sysfs_int(uint8_t dev_id, char *filename, int val)
170 {
171  /* Allocate dev_id + filename */
172  char *temp;
173  if (asprintf(&temp, "/sys/bus/iio/devices/iio:device%d/%s", dev_id, filename) < 0) {
174  return;
175  }
176 
177  /* Open the file */
178  FILE *fd = fopen(temp, "w");
179  free(temp);
180 
181  if (fd == NULL) {
182  return;
183  }
184 
185  /* Write the value to the file */
186  fprintf(fd, "%d", val);
187  fclose(fd);
188 }
void adc_enable(struct adc_t *adc, uint8_t value)
Start or stop the ADC readings.
Definition: adc_arch.c:101
uint16_t buf_length
ADC buffer length.
Definition: adc_arch.h:38
unsigned short uint16_t
Definition: types.h:16
int adc_read(struct adc_t *adc, uint16_t *buf, uint16_t size)
Read the ADC buffer from the driver.
Definition: adc_arch.c:113
static void write_sysfs_int(uint8_t dev_id, char *filename, int val)
Write an int to a sysfs file.
Definition: adc_arch.c:169
static uint32_t s
adc1_channels
Definition: adc_arch.h:41
uint8_t channels_cnt
Amount of channels.
Definition: adc_arch.h:37
void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
Link between ChibiOS ADC drivers and Paparazzi adc_buffers.
Definition: adc_arch.c:281
arch independent ADC (Analog to Digital Converter) API
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:53
Definition: adc_arch.h:34
uint8_t * channels
Channels used in the iio device.
Definition: adc_arch.h:36
uint16_t val[TCOUPLE_NB]
static void adc_dev_init(struct adc_t *adc)
Initialize an ADC device.
Definition: adc_arch.c:148
uint8_t dev_id
The iio device ID.
Definition: adc_arch.h:35
unsigned char uint8_t
Definition: types.h:14
int fd
Definition: serial.c:26
void adc_init(void)
Adc init.
Definition: adc_arch.c:304