Skip to content

Commit 165301f

Browse files
authored
Merge pull request #108 from MatthewWest/develop
Support for PWM in 4.1+
2 parents eb0b347 + 565f2dc commit 165301f

21 files changed

+1050
-328
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
packages = find_packages(),
4242
py_modules = ['Adafruit_I2C'],
4343
ext_modules = [Extension('Adafruit_BBIO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
44-
Extension('Adafruit_BBIO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
44+
Extension('Adafruit_BBIO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/c_pinmux.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
4545
Extension('Adafruit_BBIO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
4646
Extension('Adafruit_BBIO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
4747
Extension('Adafruit_BBIO.UART', ['source/py_uart.c', 'source/c_uart.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41)] )

source/c_adc.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,55 +38,58 @@ SOFTWARE.
3838

3939
int adc_initialized = 0;
4040

41-
int initialize_adc(void)
41+
BBIO_err initialize_adc(void)
4242
{
4343
#ifdef BBBVERSION41
4444
char test_path[49];
4545
#else
4646
char test_path[40];
4747
#endif
4848
FILE *fh;
49+
BBIO_err err;
50+
4951
if (adc_initialized) {
50-
return 1;
52+
return BBIO_OK;
5153
}
5254

5355
#ifdef BBBVERSION41
54-
if (load_device_tree("BB-ADC")) {
56+
err = load_device_tree("BB-ADC");
57+
if (err == BBIO_OK) {
5558
strncat(adc_prefix_dir, "/sys/bus/iio/devices/iio:device0/in_voltage", sizeof(adc_prefix_dir));
5659
snprintf(test_path, sizeof(test_path), "%s%d_raw", adc_prefix_dir, 1);
5760
fh = fopen(test_path, "r");
5861

5962
if (!fh) {
60-
puts("wiiii");
61-
return 0;
63+
return BBIO_SYSFS;
6264
}
6365
fclose(fh);
6466

6567
adc_initialized = 1;
66-
return 1;
68+
return BBIO_OK;
6769
}
6870
#else
69-
if (load_device_tree("cape-bone-iio")) {
71+
err = load_device_tree("cape-bone-iio");
72+
if (err == BBIO_OK) {
7073
build_path("/sys/devices", "ocp.", ocp_dir, sizeof(ocp_dir));
7174
build_path(ocp_dir, "helper.", adc_prefix_dir, sizeof(adc_prefix_dir));
7275
strncat(adc_prefix_dir, "/AIN", sizeof(adc_prefix_dir));
7376
snprintf(test_path, sizeof(test_path), "%s%d", adc_prefix_dir, 0);
7477
fh = fopen(test_path, "r");
7578

7679
if (!fh) {
77-
return 0;
80+
return BBIO_SYSFS;
7881
}
7982
fclose(fh);
8083

8184
adc_initialized = 1;
82-
return 1;
85+
return BBIO_OK;
8386
}
8487
#endif
8588

86-
return 0;
89+
return BBIO_GEN;
8790
}
8891

89-
int read_value(unsigned int ain, float *value)
92+
BBIO_err read_value(unsigned int ain, float *value)
9093
{
9194
FILE * fh;
9295
#ifdef BBBVERSION41
@@ -109,7 +112,7 @@ int read_value(unsigned int ain, float *value)
109112

110113
// Likely a bad path to the ocp device driver
111114
if (!fh) {
112-
return -1;
115+
return BBIO_SYSFS;
113116
}
114117

115118
fseek(fh, 0, SEEK_SET);
@@ -121,22 +124,22 @@ int read_value(unsigned int ain, float *value)
121124
try_count++;
122125
}
123126

124-
if (read_successful) return 1;
127+
if (read_successful) return BBIO_OK;
125128

126129
// Fall through and fail
127-
return -1;
130+
return BBIO_GEN;
128131
}
129132

130-
int adc_setup()
133+
BBIO_err adc_setup(void)
131134
{
132135
return initialize_adc();
133136
}
134137

135-
void adc_cleanup(void)
138+
BBIO_err adc_cleanup(void)
136139
{
137140
#ifdef BBBVERSION41
138-
unload_device_tree("BB-ADC");
141+
return unload_device_tree("BB-ADC");
139142
#else
140-
unload_device_tree("cape-bone-iio");
143+
return unload_device_tree("cape-bone-iio");
141144
#endif
142145
}

source/c_adc.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
2222
*/
2323

24+
#ifndef BBIO_C_ADC_H
25+
#define BBIO_C_ADC_H
26+
#include "common.h"
27+
2428
extern int adc_initialized;
2529

26-
int adc_setup(void);
27-
int read_value(unsigned int ain, float *value);
28-
void adc_cleanup(void);
30+
BBIO_err adc_setup(void);
31+
BBIO_err read_value(unsigned int ain, float *value);
32+
BBIO_err adc_cleanup(void);
33+
#endif

source/c_pinmux.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#include "c_pinmux.h"
5+
#include "common.h"
6+
7+
8+
BBIO_err set_pin_mode(const char *key, const char *mode)
9+
{
10+
// char ocp_dir[] defined in common.h
11+
char path[60]; // "/sys/devices/platform/ocp/ocp:P#_##_pinmux/state"
12+
char pinmux_dir[20]; // "ocp:P#_##_pinmux"
13+
FILE *f = NULL;
14+
15+
#ifndef BBBVERSION41
16+
BBIO_err err;
17+
err = build_path("/sys/devices", "ocp", ocp_dir, sizeof(ocp_dir));
18+
if (err != BBIO_OK) {
19+
return err;
20+
}
21+
#else
22+
strncpy(ocp_dir, "/sys/devices/platform/ocp", sizeof(ocp_dir));
23+
#endif
24+
25+
snprintf(pinmux_dir, sizeof(pinmux_dir), "ocp:%s_pinmux", key);
26+
snprintf(path, sizeof(path), "%s/%s/state", ocp_dir, pinmux_dir);
27+
28+
f = fopen(path, "w");
29+
if (NULL == f) {
30+
return BBIO_ACCESS;
31+
}
32+
33+
fprintf(f, "%s", mode);
34+
fclose(f);
35+
36+
return BBIO_OK;
37+
}

source/c_pinmux.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright (c) 2016 Adafruit
3+
Author: Matthew West
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+
of the Software, and to permit persons to whom the Software is furnished to do
10+
so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
*/
23+
24+
#ifndef C_PINMUX_H
25+
#define C_PINMUX_H
26+
27+
#include "common.h"
28+
29+
BBIO_err set_pin_mode(const char *key, const char *mode);
30+
31+
#endif

0 commit comments

Comments
 (0)