Skip to content

Commit 61af64e

Browse files
committed
use set_pin_mode() to set uart pinmux (#158)
Use set_pin_mode() to set pinmux for UART RX and TX pins. This is similar effect as running the config-pin utility. Signed-off-by: Drew Fustini <[email protected]>
1 parent f64382b commit 61af64e

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@
5454
ext_modules = [Extension('Adafruit_BBIO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/c_pinmux.c', 'source/constants.c', 'source/common.c'], **extension_args),
5555
Extension('Adafruit_BBIO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/c_pinmux.c', 'source/constants.c', 'source/common.c'], **extension_args),
5656
Extension('Adafruit_BBIO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], **extension_args),
57-
Extension('Adafruit_BBIO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c'], **extension_args),
58-
Extension('Adafruit_BBIO.UART', ['source/py_uart.c', 'source/c_uart.c', 'source/constants.c', 'source/common.c'], **extension_args)] )
57+
Extension('Adafruit_BBIO.SPI', ['source/spimodule.c', 'source/c_pinmux.c', 'source/constants.c', 'source/common.c'], **extension_args),
58+
Extension('Adafruit_BBIO.UART', ['source/py_uart.c', 'source/c_pinmux.c', 'source/c_uart.c', 'source/constants.c', 'source/common.c'], **extension_args)] )
5959

source/common.c

-8
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,6 @@ pins_t table[] = {
263263
{ NULL, NULL, 0, 0, 0 }
264264
};
265265

266-
typedef struct uart_t {
267-
const char *name;
268-
const char *path;
269-
const char *dt;
270-
const char *rx;
271-
const char *tx;
272-
} uart_t;
273-
274266
uart_t uart_table[] = {
275267
{ "UART1", "/dev/ttyO1", "ADAFRUIT-UART1", "P9_26", "P9_24"},
276268
{ "UART2", "/dev/ttyO2", "ADAFRUIT-UART2", "P9_22", "P9_21"},

source/common.h

+11
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ typedef struct pwm_t {
6464
const char *key; // Pin name eg P9_21
6565
} pwm_t;
6666

67+
68+
typedef struct uart_t {
69+
const char *name;
70+
const char *path;
71+
const char *dt;
72+
const char *rx;
73+
const char *tx;
74+
} uart_t;
75+
76+
extern uart_t uart_table[];
77+
6778
extern int gpio_mode;
6879
extern int gpio_direction[120];
6980

source/py_uart.c

+24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ SOFTWARE.
2525
#include "constants.h"
2626
#include "common.h"
2727
#include "c_uart.h"
28+
#include "c_pinmux.h"
2829

2930
const char *valid_uarts[4] = {"UART1", "UART2", "UART4", "UART5"};
3031

@@ -61,6 +62,29 @@ static PyObject *py_setup_uart(__attribute__ ((unused)) PyObject *self, PyObject
6162
return NULL;
6263
}
6364

65+
#ifdef BBBVERSION41
66+
uart_t *p;
67+
for (p = uart_table; p->name != NULL; ++p) {
68+
if (strcmp(p->name, channel) == 0) {
69+
err = set_pin_mode(p->rx, "uart");
70+
//Check if set_pin_mode() returned no error
71+
if (err != BBIO_OK) {
72+
fprintf(stderr, "py_setup_uart(%s): set_pin_mode() failed for pin=%s", channel, p->rx);
73+
PyErr_SetString(PyExc_ValueError, "Set pin mode failed for uart channel.");
74+
return NULL;
75+
}
76+
77+
err = set_pin_mode(p->tx, "uart");
78+
//Check if set_pin_mode() returned no error
79+
if (err != BBIO_OK) {
80+
fprintf(stderr, "py_setup_uart(%s): set_pin_mode() failed for pin=%s", channel, p->tx);
81+
PyErr_SetString(PyExc_ValueError, "Set pin mode failed for uart channel.");
82+
return NULL;
83+
}
84+
}
85+
}
86+
#endif
87+
6488
Py_RETURN_NONE;
6589
}
6690

0 commit comments

Comments
 (0)