BBB bonescript, configuring muxes

Hi,

I’m trying to set a bunch of pins to be GPIO on one side of the P8 connector. I’m using the following script:

`
var b = require(‘bonescript’);

// Set 8 pins into HIGH outputs (to turn relays off since they are active low):
var numPins = 8;
var starting_pin = 12;
var connector = “P8”;
var skip = 2; // every second pin
pins = new Array(numPins);
for (var i = 0; i < numPins; i++) {
name = “P8_” + (i*skip + starting_pin);
gpiomux = b.bone.pins[name].options.slice(-1)[0]; // last option is GPIO
console.log(“Pin name is " + name + " and initialing to gpio=” + gpiomux + " and setting to HIGH");
b.bone.pins[name].mux = gpiomux;
b.digitalWrite(name, b.HIGH);
}

`

But I get errors on some pins when doing this:

Pin name is P8_12 and initialized to gpio=gpio1_12 and set to HIGH
Pin name is P8_14 and initialized to gpio=gpio0_26 and set to HIGH
Pin name is P8_16 and initialized to gpio=gpio1_14 and set to HIGH
error: Unable to write to /sys/class/gpio/gpio46/value
Pin name is P8_18 and initialized to gpio=gpio2_1 and set to HIGH
error: Unable to write to /sys/class/gpio/gpio65/value
Pin name is P8_20 and initialized to gpio=gpio1_31 and set to HIGH
error: Unable to write to /sys/class/gpio/gpio63/value
Pin name is P8_22 and initialized to gpio=gpio1_5 and set to HIGH
error: Unable to write to /sys/class/gpio/gpio37/value
Pin name is P8_24 and initialized to gpio=gpio1_1 and set to HIGH
error: Unable to write to /sys/class/gpio/gpio33/value
Pin name is P8_26 and initialized to gpio=gpio1_29 and set to HIGH
error: Unable to write to /sys/class/gpio/gpio61/value

Does anyone know why I’m getting these errors and what is the best way to set a bunch of pins to GPIO outputs?

Glen

Hi Glen,

'am no gpio expert, but something in the output is not making sense:

  1. the console.log entry you are building in the code,does not tally with the output msg string displayed. Could your script and output be mismatched ?
  2. I ran the getPinMode() script in the sample page served by the running bbb, and for pin P8_16 (the first one where you are seeing an error). The response is like so (of interest is the pin name GPIO1_14, which matched the docs):

mux = 7
pullup = pulldown
slew = fast
options = gpmc_ad14,lcd_data17,mmc1_dat6,mmc2_dat2,eqep2_index,pr1_mii0_txd0,pr1_pru0_pru_r31_14,gpio1_14
pin = P8_16
name = GPIO1_14
err = undefined

However, in your output, it is complaining that it could not set gpio46 HIGH:

error: Unable to write to /sys/class/gpio/gpio46/value

Could there be a mapping error issue here? Nowhere can I see documented a map between GPIO1_14 and gpio46
if not, could there be an existing “system” assignment for that pin, which you cannot alter ?

Was using
/venkat

On the BeagleBone (white) the way to figure out the name of the file is by taking the first number (1 in this case) and multiplying by 32 and then add the second number (14 in this case)… gpio46 is correct, but some GPIO pins are not allowed to be used. Not sure if this is one or not.

Hope this helps.

Cheers,

Gregg

Hi Venkat,

Thanks for the tips.

For (1) I hand edited my log statements when I posted to change the tense of the sentence…no issue here except a lesson for me not to do that :slight_smile:

For (2) I tried using pinMode too and this is what I get:

`
var b = require(‘bonescript’);

/////////////////////////////////////
// Setup:
/////////////////////////////////////
function printStatus(x) {
console.log('value = ’ + x.value);
console.log('err = ’ + x.err);
console.log(x);
}

function printPinMux(x) {
console.log(x.pin + ': mux = ’ + x.mux + ', pullup = ’ + x.pullup + ‘, slew = ’ + x.slew + ‘, options = ’ + x.options.join(’,’) + ', err = ’ + x.err);
}

// Set pins to HIGH outputs (to turn relays off since they are active low):
var numPins = 40;
var starting_pin = 3;
var connector = “P8”;
var skip = 1; // every second pin
var state = b.HIGH;
pins = new Array(numPins);
console.log(“Initializing pins to GPIO and setting to HIGH”);
for (var i = 0; i < numPins; i++) {
name = connector + “_” + (i*skip + starting_pin);
pins[i] = name;
b.pinMode(name, b.OUTPUT, 7, ‘pullup’, ‘fast’);
b.getPinMode(name, printPinMux);
b.digitalWrite(name, state);
}

//////////////////////////////////////
// Loop:
//////////////////////////////////////
function loop() {
var state = b.HIGH;
return function () {
if (state == b.HIGH) state = b.LOW;
else state = b.HIGH;
console.log("Setting pins “+pins+” to " + state);
for (var i = 0; i < numPins; i++) {
name = pins[i];
b.digitalWrite(name, state);
}
}
}

// Start loop:
setInterval(loop(), 1000);

`

And the output is:

Initializing pins to GPIO and setting to HIGH
P8_3: mux = 1, pullup = pullup, slew = fast, options = gpmc_ad6,mmc1_dat6,NA,NA,NA,NA,NA,gpio1_6, err = undefined
P8_4: mux = 1, pullup = pullup, slew = fast, options = gpmc_ad7,mmc1_dat7,NA,NA,NA,NA,NA,gpio1_7, err = undefined
P8_5: mux = 1, pullup = pullup, slew = fast, options = gpmc_ad2,mmc1_dat2,NA,NA,NA,NA,NA,gpio1_2, err = undefined
P8_6: mux = 1, pullup = pullup, slew = fast, options = gpmc_ad3,mmc1_dat3,NA,NA,NA,NA,NA,gpio1_3, err = undefined
P8_7: mux = 7, pullup = pullup, slew = fast, options = gpmc_advn_ale,NA,NA,NA,NA,NA,NA,mmc1_sdcd, err = undefined
P8_8: mux = 7, pullup = pullup, slew = fast, options = gpmc_oen_ren,NA,NA,NA,NA,NA,NA,gpio2_3, err = undefined
P8_9: mux = 7, pullup = pullup, slew = fast, options = gpmc_ben0_cle,NA,NA,NA,NA,NA,NA,gpio2_5, err = undefined
P8_10: mux = 7, pullup = pullup, slew = fast, options = gpmc_wen,NA,NA,NA,NA,NA,NA,gpio2_4, err = undefined
P8_11: mux = 7, pullup = pulldown, slew = fast, options = gpmc_ad13,lcd_data18,mmc1_dat5,mmc2_dat1,eqep2B_in,pr1_mii0_txd,pr1_pru0_pru_r30_15,gpio1_13, err = undefined
P8_12: mux = 7, pullup = pulldown, slew = fast, options = gpmc_ad12,lcd_data19,mmc1_dat4,mmc2_dat0,eqep2a_in,pr1_mii0_txd2,pr1_pru0_pru_r30_14,gpio1_12, err = undefined
P8_13: mux = 7, pullup = pulldown, slew = fast, options = gpmc_ad9,lcd_data22,mmc1_dat1,mmc2_dat5,ehrpwm2B,pr1_mii0_col,NA,gpio0_23, err = undefined
P8_14: mux = 7, pullup = pulldown, slew = fast, options = gpmc_ad10,lcd_data21,mmc1_dat2,mmc2_dat6,ehrpwm2_tripzone_input,pr1_mii0_txen,NA,gpio0_26, err = undefined
P8_16: mux = 7, pullup = pulldown, slew = fast, options = gpmc_ad14,lcd_data17,mmc1_dat6,mmc2_dat2,eqep2_index,pr1_mii0_txd0,pr1_pru0_pru_r31_14,gpio1_14, err = undefined
P8_15: mux = 7, pullup = pulldown, slew = fast, options = gpmc_ad15,lcd_data16,mmc1_dat7,mmc2_dat3,eqep2_strobe,pr1_ecap0_ecap_capin_apwm_o,pr1_pru0_pru_r31_15,gpio1_15, err = undefined
P8_17: mux = 7, pullup = pulldown, slew = fast, options = gpmc_ad11,lcd_data20,mmc1_dat3,mmc2_dat7,ehrpwm0_synco,pr1_mii0_txd3,NA,gpio0_27, err = undefined
P8_18: mux = 7, pullup = pulldown, slew = fast, options = gpmc_clk,lcd_memory_clk_mux,NA,mmc2_clk,NA,NA,mcasp0_fsr,gpio2_1, err = undefined
P8_20: mux = 2, pullup = pullup, slew = fast, options = gpmc_csn2,gpmc_be1n,mmc1_cmd,pr1_edio_data_in7,pr1_edio_data_out7,pr1_pru1_pru_r30_13,pr1_pru1_pru_r31_13,gpio1_31, err = undefined
P8_19: mux = 7, pullup = pulldown, slew = fast, options = gpmc_ad8,lcd_data23,mmc1_dat0,mmc2_dat4,ehrpwm2A,pr1_mii_mt0_clk,NA,gpio0_22, err = undefined
P8_21: mux = 2, pullup = pullup, slew = fast, options = gpmc_csn1,gpmc_clk,mmc1_clk,pr1_edio_data_in6,pr1_edio_data_out6,pr1_pru1_pru_r30_12,pr1_pru1_pru_r31_12,gpio1_30, err = undefined
P8_22: mux = 1, pullup = pullup, slew = fast, options = gpmc_ad5,mmc1_dat5,NA,NA,NA,NA,NA,gpio1_5, err = undefined
P8_23: mux = 1, pullup = pullup, slew = fast, options = gpmc_ad4,mmc1_dat4,NA,NA,NA,NA,NA,gpio1_4, err = undefined
P8_24: mux = 1, pullup = pullup, slew = fast, options = gpmc_ad1,mmc1_dat1,NA,NA,NA,NA,NA,gpio1_1, err = undefined
P8_25: mux = 1, pullup = pullup, slew = fast, options = gpmc_ad0,mmc1_dat0,NA,NA,NA,NA,NA,gpio1_0, err = undefined
P8_26: mux = 7, pullup = pullup, slew = fast, options = gpmc_csn0,NA,NA,NA,NA,NA,NA,gpio1_29, err = undefined
P8_27: mux = 0, pullup = pulldown, slew = fast, options = lcd_vsync,gpmc_a8,NA,pr1_edio_data_in2,pr1_edio_data_out2,pr1_pru1_pru_r30_8,pr1_pru1_pru_r31_8,gpio2_22, err = undefined
P8_28: mux = 0, pullup = pulldown, slew = fast, options = lcd_pclk,gpmc_a10,pr1_mii0_crs,pr1_edio_data_in4,pr1_edio_data_out4,pr1_pru1_pru_r30_10,pr1_pru1_pru_r31_10,gpio2_24, err = undefined
P8_29: mux = 0, pullup = pulldown, slew = fast, options = lcd_hsync,gpmc_a9,NA,pr1_edio_data_in3,pr1_edio_data_out3,pr1_pru1_pru_r30_9,pr1_pru1_pru_r31_9,gpio2_23, err = undefined
P8_30: mux = 0, pullup = pulldown, slew = fast, options = lcd_ac_bias_en,gpmc_a11,pr1_mii1_crs,pr1_edio_data_in5,pr1_edio_data_out5,pr1_pru1_pru_r30_11,pr1_pru1_pru_r31_11,gpio2_25, err = undefined
P8_31: mux = 0, pullup = disabled, slew = fast, options = lcd_data14,gpmc_a18,NA,mcasp0_axr1,NA,NA,NA,gpio0_10, err = undefined
P8_32: mux = 0, pullup = disabled, slew = fast, options = lcd_data15,gpmc_a19,NA,mcasp0_ahclkx,mcasp0_axr3,NA,NA,gpio0_11, err = undefined
P8_33: mux = 0, pullup = disabled, slew = fast, options = lcd_data13,gpmc_a17,NA,mcasp0_fsr,mcasp0_axr3,NA,NA,gpio0_9, err = undefined
P8_34: mux = 0, pullup = disabled, slew = fast, options = lcd_data11,gpmc_a15,NA,mcasp0_ahclkr,mcasp0_axr2,NA,NA,gpio2_17, err = undefined
P8_35: mux = 0, pullup = disabled, slew = fast, options = lcd_data12,gpmc_a16,NA,mcasp0_aclkr,mcasp0_axr2,NA,NA,gpio0_8, err = undefined
P8_36: mux = 0, pullup = disabled, slew = fast, options = lcd_data10,gpmc_a14,ehrpwm1A,mcasp0_axr0,mcasp0_axr0,pr1_mii0_rxd1,uart3_ctsn,gpio2_16, err = undefined
P8_37: mux = 0, pullup = disabled, slew = fast, options = lcd_data8,gpmc_a12,NA,mcasp0_aclkx,NA,NA,uart2_ctsn,gpio2_14, err = undefined
P8_38: mux = 0, pullup = disabled, slew = fast, options = lcd_data9,gpmc_a13,NA,mcasp0_fsx,NA,NA,uart2_rtsn,gpio2_15, err = undefined
P8_39: mux = 0, pullup = disabled, slew = fast, options = lcd_data6,gpmc_a6,pr1_edio_data_in6,eqep2_index,pr1_edio_data_out6,pr1_pru1_pru_r30_6,pr1_pru1_pru_r31_6,gpio2_12, err = undefined
P8_40: mux = 0, pullup = disabled, slew = fast, options = lcd_data7,gpmc_a7,pr1_edio_data_in7,eqep2_strobe,pr1_pru1_pru_r30_7,pr1_pru_pru1_r30_7,pr1_pru1_pru_r31_7,gpio2_13, err = undefined
P8_41: mux = 0, pullup = disabled, slew = fast, options = lcd_data4,gpmc_a4,pr1_mii0_txd1,eQEP2A_in,NA,pr1_pru1_pru_r30_4,pr1_pru1_pru_r31_4,gpio2_10, err = undefined
P8_42: mux = 0, pullup = disabled, slew = fast, options = lcd_data5,gpmc_a5,pr1_mii0_txd0,eqep2b_in,NA,pr1_pru1_pru_r30_5,pr1_pru1_pru_r31_5,gpio2_11, err = undefined
Setting pins P8_3,P8_4,P8_5,P8_6,P8_7,P8_8,P8_9,P8_10,P8_11,P8_12,P8_13,P8_14,P8_15,P8_16,P8_17,P8_18,P8_19,P8_20,P8_21,P8_22,P8_23,P8_24,P8_25,P8_26,P8_27,P8_28,P8_29,P8_30,P8_31,P8_32,P8_33,P8_34,P8_35,P8_36,P8_37,P8_38,P8_39,P8_40,P8_41,P8_42 to 0
Setting pins P8_3,P8_4,P8_5,P8_6,P8_7,P8_8,P8_9,P8_10,P8_11,P8_12,P8_13,P8_14,P8_15,P8_16,P8_17,P8_18,P8_19,P8_20,P8_21,P8_22,P8_23,P8_24,P8_25,P8_26,P8_27,P8_28,P8_29,P8_30,P8_31,P8_32,P8_33,P8_34,P8_35,P8_36,P8_37,P8_38,P8_39,P8_40,P8_41,P8_42 to 1

Hooking up an LED to blink the output, only the pins with mux=7 are behaving as outputs. The ‘pullup’ setter doesn’t seem to do anything but the reported value seems accurate.

I’m on kernel 3.8.11.

Glen

as far as I understand the situation, at the moment you cant. Bone script tries to use the "legacy" way of "omap_mux" to change the pinmux and that fails on 3.8.x

it does not yet have code to handle it the new way with DT fragments, so
for now, bonescript can only use the pins that are already setup as GPIO at boot.

Jason might be able to comment on when Bonescript will be "adapted" to the new ways

Hi Vladimir,

Is there any way (other than bonescript) to configure the IO pins and use them? I’d prefer command line tools. I’d like to use cron to control the pins.

Thanks,

Glen

The new way to do pinmuxing is to either modify the (DT) device tree that gets loaded when the kernel boots, or to load DT overlay "fragments" at run time, much like the mechanism that is used when the kernel detects and configures a cape that needs muxing.

The exact steps to do this I cannot give you as I have not ventured too deeply into the DT world yet, others here on the list might be more capable of giving a more verbose answer.

there is also a "pinmux-helper" driver in newer kernels that is supposed to help here, but I cannot comment on that either (for now)

sorry to be so vague right now

Regards,

Vladimir

Hi Vladimir,

Thanks for the info. I found out how to control the pins on the command line. You first need to configure the pins for outputs (which I don’t know how to do on the command line yet) and then you can do stuff like:

echo 1 > /sys/devices/virtual/gpio/gpio44/value # set P8_12 high

sleep 1
echo 0 > /sys/devices/virtual/gpio/gpio44/value # set P8_12 low

I’ll have to do some research. I found this as a starting point:

https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/Documentation/gpio.txt?id=refs/tags/v3.8.11

Any pointers would be appreciated. Thanks for your help.

Glen