3 * @purpose Implementation of BBB pin control functions and structures
4 * THIS CODE IS NOT THREADSAFE
16 * Structure to represent a GPIO pin
17 * Note: Only accessable to this file; to use the functions pass a GPIOId
26 * Structure to represent an ADC pin
27 * Note: Only accessable to this file; to use the functions pass a ADCId
35 * Structure to represent a PWM pin
36 * Note: Only accessable to this file; to use the functions pass a PWMId
46 /** Array of GPIO pins **/
47 static GPIO_Pin g_gpio[GPIO_NUM_PINS] = {{0}};
48 /** Array of ADC pins **/
49 static ADC_Pin g_adc[ADC_NUM_PINS] = {{0}};
50 /** Array of PWM pins **/
51 static PWM_Pin g_pwm[PWM_NUM_PINS] = {{0}};
53 static char g_buffer[BUFSIZ] = "";
59 * Export a GPIO pin and open the file descriptors
61 void GPIO_Export(int pin)
63 if (pin < 0 || pin > GPIO_NUM_PINS)
64 Fatal("Invalid pin number %d", pin);
69 sprintf(g_buffer, "%s/export", GPIO_DEVICE_PATH);
70 FILE * export = fopen(g_buffer, "w");
72 Fatal("Couldn't open %s to export GPIO pin %d - %s", g_buffer, pin, strerror(errno));
74 fprintf(export, "%d", pin);
77 // Setup direction file descriptor
78 sprintf(g_buffer, "%s/gpio%d/direction", GPIO_DEVICE_PATH, pin);
79 g_gpio[pin].fd_direction = open(g_buffer, O_RDWR);
80 if (g_gpio[pin].fd_direction < 0)
81 Fatal("Couldn't open %s for GPIO pin %d - %s", g_buffer, pin, strerror(errno));
84 // Setup value file descriptor
85 sprintf(g_buffer, "%s/gpio%d/value", GPIO_DEVICE_PATH, pin);
86 g_gpio[pin].fd_value = open(g_buffer, O_RDWR);
87 if (g_gpio[pin].fd_value < 0)
88 Fatal("Couldn't open %s for GPIO pin %d - %s", g_buffer, pin, strerror(errno));
92 * Unexport a GPIO pin and close its' file descriptors
94 void GPIO_Unexport(int pin)
97 if (pin < 0 || pin > GPIO_NUM_PINS)
98 Fatal("Invalid pin number %d", pin);
100 // Close file descriptors
101 close(g_gpio[pin].fd_value);
102 close(g_gpio[pin].fd_direction);
106 if (g_buffer[0] == '\0')
107 sprintf(g_buffer, "%s/unexport", GPIO_DEVICE_PATH);
108 FILE * export = fopen(g_buffer, "w");
110 Fatal("Couldn't open %s to export GPIO pin %d - %s", g_buffer, pin, strerror(errno));
112 fprintf(export, "%d", pin);
120 * Export all PWM pins and open file descriptors
121 * @param pin - The pin number
123 void PWM_Export(int pin)
125 if (pin < 0 || pin > PWM_NUM_PINS)
126 Fatal("Invalid pin number %d", pin);
129 sprintf(g_buffer, "%s/export", PWM_DEVICE_PATH);
130 FILE * export = fopen(g_buffer, "w");
132 Fatal("Couldn't open %s to export PWM pin %d - %s", g_buffer, pin, strerror(errno));
134 fprintf(export, "%d\n", pin);
137 // Open file descriptors
138 sprintf(g_buffer, "%s/pwm%d/run", PWM_DEVICE_PATH, pin);
139 g_pwm[pin].fd_run = open(g_buffer, O_WRONLY);
140 if (g_pwm[pin].fd_run < 0)
141 Fatal("Couldn't open %s for PWM pin %d - %s", g_buffer, pin, strerror(errno));
143 sprintf(g_buffer, "%s/pwm%d/polarity",PWM_DEVICE_PATH, pin);
144 g_pwm[pin].fd_polarity = open(g_buffer, O_WRONLY);
145 if (g_pwm[pin].fd_polarity < 0)
146 Fatal("Couldn't open %s for PWM pin %d - %s", g_buffer, pin, strerror(errno));
148 sprintf(g_buffer, "%s/pwm%d/period_ns",PWM_DEVICE_PATH, pin);
149 g_pwm[pin].file_period = fopen(g_buffer, "w");
150 if (g_pwm[pin].file_period == NULL)
151 Fatal("Couldn't open %s for PWM pin %d - %s", g_buffer, pin, strerror(errno));
153 sprintf(g_buffer, "%s/pwm%d/duty_ns",PWM_DEVICE_PATH, pin);
154 g_pwm[pin].file_duty = fopen(g_buffer, "w");
155 if (g_pwm[pin].file_duty == NULL)
156 Fatal("Couldn't open %s for PWM pin %d - %s", g_buffer, pin, strerror(errno));
158 // Don't buffer the streams
159 setbuf(g_pwm[pin].file_period, NULL);
160 setbuf(g_pwm[pin].file_duty, NULL);
166 * Unexport a PWM pin and close its file descriptors
167 * @param pin - The pin number
169 void PWM_Unexport(int pin)
171 if (pin < 0 || pin > PWM_NUM_PINS)
172 Fatal("Invalid pin number %d", pin);
174 // Close the file descriptors
175 close(g_pwm[pin].fd_polarity);
176 close(g_pwm[pin].fd_run);
177 fclose(g_pwm[pin].file_period);
178 fclose(g_pwm[pin].file_duty);
181 sprintf(g_buffer, "%s/unexport", PWM_DEVICE_PATH);
182 FILE * export = fopen(g_buffer, "w");
184 Fatal("Couldn't open %s to unexport PWM pin %d - %s", g_buffer, pin, strerror(errno));
186 fprintf(export, "%d", pin);
193 * Export ADC pins; http://beaglebone.cameon.net/home/reading-the-analog-inputs-adc
194 * Can't use sysfs like GPIO or PWM pins
195 * Bloody annoying how inconsistent stuff is on the Beaglebone
199 for (int i = 0; i < ADC_NUM_PINS; ++i)
201 sprintf(g_buffer, "%s/AIN%d", g_options.adc_device_path, i);
202 g_adc[i].fd_value = open(g_buffer, O_RDONLY);
203 if (g_adc[i].fd_value < 0)
204 Fatal("Couldn't open ADC %d device file %s - %s", i, g_buffer, strerror(errno));
206 //setbuf(g_adc[i].file_value, NULL);
216 for (int i = 0; i < ADC_NUM_PINS; ++i)
217 close(g_adc[i].fd_value);
222 * @param pin - The pin to set. MUST have been exported before calling this function.
224 void GPIO_Set(int pin, bool value)
226 if (pwrite(g_gpio[pin].fd_direction, "out", 3, 0) != 3)
227 Fatal("Couldn't set GPIO %d direction - %s", pin, strerror(errno));
229 char c = '0' + (value);
230 if (pwrite(g_gpio[pin].fd_value, &c, 1, 0) != 1)
231 Fatal("Couldn't read GPIO %d value - %s", pin, strerror(errno));
236 * Read from a GPIO Pin
237 * @param pin - The pin to read
239 bool GPIO_Read(int pin)
241 if (pwrite(g_gpio[pin].fd_direction, "in", 2, 0) != 2)
242 Fatal("Couldn't set GPIO %d direction - %s", pin, strerror(errno));
244 if (pread(g_gpio[pin].fd_value, &c, 1, 0) != 1)
245 Fatal("Couldn't read GPIO %d value - %s", pin, strerror(errno));
253 * @param pin - The pin to activate
254 * @param polarity - if true, pin is active high, else active low
255 * @param period - The period in ns
256 * @param duty - The time the pin is active in ns
258 void PWM_Set(int pin, bool polarity, long period, long duty)
260 // Have to stop PWM before changing it
261 if (pwrite(g_pwm[pin].fd_run, "0", 1, 0) != 1)
262 Fatal("Couldn't stop PWM %d - %s", pin, strerror(errno));
264 char c = '0' + polarity;
265 if (pwrite(g_pwm[pin].fd_polarity, &c, 1, 0) != 1)
266 Fatal("Couldn't set PWM %d polarity - %s", pin, strerror(errno));
269 rewind(g_pwm[pin].file_period);
270 rewind(g_pwm[pin].file_duty);
272 if (fprintf(g_pwm[pin].file_duty, "%lu", duty) == 0)
273 Fatal("Couldn't set duty cycle for PWM %d - %s", pin, strerror(errno));
275 if (fprintf(g_pwm[pin].file_period, "%lu", period) == 0)
276 Fatal("Couldn't set period for PWM %d - %s", pin, strerror(errno));
278 if (pwrite(g_pwm[pin].fd_run, "1", 1, 0) != 1)
279 Fatal("Couldn't start PWM %d - %s", pin, strerror(errno));
284 * Deactivate a PWM pin
285 * @param pin - The pin to turn off
287 void PWM_Stop(int pin)
289 if (pwrite(g_pwm[pin].fd_run, "0", 1, 0) != 1)
290 Fatal("Couldn't stop PWM %d - %s", pin, strerror(errno));
296 * @param id - The ID of the ADC pin to read
297 * @returns - The reading of the ADC channel
301 char adc_str[ADC_DIGITS] = "";
302 lseek(g_adc[id].fd_value, 0, SEEK_SET);
305 for (i = 0; i < ADC_DIGITS-1; ++i)
307 if (read(g_adc[id].fd_value, adc_str+i, 1) != 1)
309 if (adc_str[i] == '\n')
317 int val = strtol(adc_str, &end, 10);
320 Log(LOGERR, "Read non integer from ADC %d - %s", id, adc_str);