37 Replies Latest reply on Nov 3, 2019 4:00 AM by stephensmith1211

    Accessing GPIO and PWM on BeagleBone AI

    sjmill01

      I'm running through the BeagleBone examples for the AI, but the ones that attempt to read or write to pins throw errors in both Python and bonescript.

       

      Here is the output from analoginout.js:

       

      Hit ^C to stop

      info: No pinmux for P9_14

      fs.js:733

        var r = binding.read(fd, buffer, offset, length, position);

                       ^

      Error: ETIMEDOUT: connection timed out, read

          at Error (native)

          at Object.fs.readSync (fs.js:733:19)

          at tryReadSync (fs.js:488:20)

          at Object.fs.readFileSync (fs.js:528:19)

          at Object.readAIN (/usr/local/lib/node_modules/bonescript/src/hw_mainline.js:287:30)

          at Object.f.analogRead (/usr/local/lib/node_modules/bonescript/src/index.js:330:19)

          at Timeout.loop [as _onTimeout] (/var/lib/cloud9/BeagleBone/AI/analogInOut.js:23:19)

          at ontimeout (timers.js:386:11)

          at tryOnTimeout (timers.js:250:5)

          at Timer.listOnTimeout (timers.js:214:5)

       

      Is there a config I need to do first?  I see other's in the world with this issue on older BeagleBone's and the responses vary considerably.

       

      This is the code:

      const b = require('bonescript');
      
      
      const inputPin  = "A3";
      const outputPin = "P9_14";
      
      
      console.log('Hit ^C to stop');
      b.pinMode(outputPin, b.ANALOG_OUTPUT);
      setTimeout(loop, 200);  // work-around to wait for PWM permissions
      
      
      function loop() {
          var value = b.analogRead(inputPin);
          process.stdout.write(inputPin + '-->' + outputPin + ': ' + (value*100).toFixed(1) + '%   \r');
          b.analogWrite(outputPin, value);
          setTimeout(loop, 10);
      }
      

       

      Thanks,

      Sean

          • Re: Accessing GPIO and PWM on BeagleBone AI
            sjmill01

            I saw these two references tonight in my hunt.  My thoughts were "this surely is handled in by the bonescript and Python libraries on the fly, right?"  "Otherwise, it would be in the comments in the header, right?"  I guess not.

             

            For the video, I didn't have the directories he showed for the capes and trees.

             

            I tried the redirect to export tricks, but still received the same error of having no pinmux.

             

            I'll run it harder through the paces tomorrow evening.  I found the tutorials just aren't out there like you find for Raspberry Pi and Arduino IDE driven microcontrollers.

             

            But, so, far, I love the idea of computations happening at the speed of the frame with the tidl libraries, love the Cloud 9 interface (1st time using it), and love being able to use it as an adhoc device while its connected to the internet.

             

            Just got to get an LED to blink and stop it from calling my black cat a ping-pong-ball. 

             

            -Sean

            2 of 2 people found this helpful
          • Re: Accessing GPIO and PWM on BeagleBone AI
            clem57

            #include <unistd.h>

            #include <stdio.h>

            using namespace std;

             

             

            int main()

            {

               FILE *export_file = NULL; //declare pointers

               FILE *IO_direction = NULL;

               FILE *IO_value = NULL;

               char str1[] = "0";

               char str2[] = "1";

               char str3[] = "out";

               char str[] = "23";

               //this part here exports gpio23

               export_file = fopen ("/sys/class/gpio/export", "w");

               fwrite (str, 1, sizeof(str), export_file);

               fclose (export_file);

               //this part here sets the direction of the pin

               IO_direction = fopen("/sys/class/gpio/gpio23/direction", "w");

               fwrite(str3, 1, sizeof(str3), IO_direction); //set the pin to HIGH

               fclose(IO_direction);

               usleep (1000000);

             

               for (int i=0; i<10; i++){ //blink LED 10 times

                    IO_value = fopen ("/sys/class/gpio/gpio23/value", "w");

                    fwrite (str2, 1, sizeof(str2), IO_value); //set the pin to HIGH

                    fclose (IO_value);

                    usleep (1000000); //delay for a second

             

             

                    IO_value = fopen ("/sys/class/gpio/gpio23/value", "w");

                    fwrite (str1, 1, sizeof(str1), IO_value); //set the pin to LOW

                    fclose (IO_value);

                    usleep (1000000); //delay for a second

                    }

            }

             

            The 3 bold lines are the setup for pin 23.

            3 of 3 people found this helpful
              • Re: Accessing GPIO and PWM on BeagleBone AI
                mayermakes

                The only thing I could find out about this issue is that the device tree files for the BB-AI are missing, there is currently no suitable file provided for the BB-AI
                all the BBB black files just brick the BB-Ai if loaded with uENv.txt. the Universal cape also does not support the processor.
                I raised an Issue on the github for the examples and messaged them,. hope Beaglebone will shortly provide a suitable overlay file /inlcuded it loaded in the next OS image release.

                thanks for posting your solution!

                For generall usage these setup procedures should be handeled in a Library or the u-boot (like intended).
                To get an urgent project working i had to do some workaround which thought me about interfacing it with USB devices, but I really would like to redo it without any additional hardware.

                1 of 1 people found this helpful
                • Re: Accessing GPIO and PWM on BeagleBone AI
                  sjmill01

                  Thanks for posting.

                   

                  Another file/shell based BoneScript workaround was in that post you sent as well:

                  /*------------------------------------------------------------------------------------
                  function to export a gpio Pin
                      Parameter : the pin that you want to export  
                  */
                  function gpio_export(pin)
                  {
                      
                      var path_export=path+"/export"; // export path
                  
                      //method where we execute the shell command
                  
                       var command = "echo "+pin.toString()+" > "+path_export //the shell command that want to do
                      
                       //execution of the command
                         process.exec(command,function (err,stdout,stderr) {
                              //if there is some error print them
                              if (err) {
                                  console.log("\n"+stderr);
                              } else {
                                  console.log(stdout);
                              }
                          });
                  
                  }
                  

                   

                  But, this makes for a negative demo if I blog and make a video on it.  Someone on the fence on going to BeagleBone could get turned off immediately.  Ping-pong-ball and I are gonna keep digging in hopes find the magic.

                   

                   

                   

                  -Sean

                  • Re: Accessing GPIO and PWM on BeagleBone AI
                    colporteur

                    Clem your endorsement of Beaglebone Al, in a recent post is in jeopardy in my eyes. Only because I lack the knowledge for the deeper dive. I was kinda expecting it to work out of the box. Not have to chew gum, rub my belly and pat my head at the same time. Are you sure Beaglebone is not an Azure board reissued as to not depend on a Microsoft connection?

                     

                    Sean 

                  • Re: Accessing GPIO and PWM on BeagleBone AI
                    mp2100

                    Sean, I am definitely not a nodejs expert, but I compared your code to the example from cloud9 on my BBBlack (I don't yet have a BB AI).  In the given example, the first 3 lines read:

                       var b = require('bonescript');

                       var inputPin = "P9_38";

                       var outputPin = "P9_14";

                     

                    Your code reads:

                    1. const b = require('bonescript');
                    2. const inputPin  = "A3";
                    3. const outputPin = "P9_14";

                     

                    Is there a reason you used constant instead of variable?

                    1 of 1 people found this helpful
                    • Re: Accessing GPIO and PWM on BeagleBone AI
                      sjmill01

                      Found a link that looks promising on establishing GPIO pins:

                           https://www.elinux.org/EBC_Exercise_41_Pin_Muxing_for_the_AI

                       

                      However, I simply bricked mine when I tried it.

                       

                      -Sean

                      1 of 1 people found this helpful
                      • Re: Accessing GPIO and PWM on BeagleBone AI
                        sjmill01

                        Jason Kridner has released a beta for the BBAI GPIO libraries for node.js:

                         

                         

                        https://groups.google.com/forum/embed/?place=forum%2Fbeagleboard&showsearch=true&showpopout=true&showtabs=false&hideforu…

                         

                        I'm try to get something going tomorrow.

                         

                        He also released an update that was to fix the tidl examples, but that one went backwards on me and now I can't get it to execute without error.  It's been posted to his support forum.

                         

                        -Sean

                        3 of 3 people found this helpful
                        • Re: Accessing GPIO and PWM on BeagleBone AI
                          clem57

                          Sean  -> So the AM5X vs AM335 requires overlays for the DTB not be done in user-space. Therefore they are done at boot time. Take a look at this discussion for the details. https://groups.google.com/forum/embed/?place=forum%2Fbeagleboard&showsearch=true&showpopout=true&showtabs=false&hideforu…

                          Clem

                          1 of 1 people found this helpful
                            • Re: Accessing GPIO and PWM on BeagleBone AI
                              sjmill01

                              Thanks, Clem.  I am definitely in unfamiliar territory here.  If I can get analog in and PWM out on top of P9.15 being a digital out, I'll be in business.

                               

                              After installing the new BoneScript library for the AI, Jason has you type this at the command line:

                              node -pe "require('bonescript').bone.getPinObject('p9.15').ai.gpio"
                              

                               

                              I don't know the background of this, but I did find that the demo worked after a reboot without having to type it again.  I tried it with P9.16, but it did not light up when ran.  No error, just didn't light up.

                               

                              I wonder what the other folks that received the BBAI are doing in the GPIO arena.  I hope they chime in.  The topic seems oddly quiet even on the BB Forum.  Based on the view count on the topic, I've been almost all of them.  This is surprising with the stats this thing will be capable of.

                               

                              -Sean

                              1 of 1 people found this helpful
                            • Re: Accessing GPIO and PWM on BeagleBone AI
                              sjmill01

                              Here's a hack for Python on the BBAI.

                               

                              Change...

                               

                              out = "P9_15"

                              to

                              out = "P8_39"

                               

                              in the blink demo and you'll get an LED blink.  It's because of the mapping difference between the BBB and BBAI.

                               

                              See ya',

                              Sean

                              2 of 2 people found this helpful
                                • Re: Accessing GPIO and PWM on BeagleBone AI
                                  sjmill01

                                  And another pin hack for the BBAI for Adafruit's Python Library BBB pins:

                                   

                                  out = "P8_13" #actually 8_22 on BBAI

                                   

                                  Here's how I'm finding it...

                                   

                                  So, using BBB's reference to P8_13 will get you use of BBAI 8_22.

                                   

                                  Two out pins are much better than none for now.  Now to get an analog in and a PWM out working.  Most likely, as soon as I get it, Adafruit will release their update. 

                                   

                                  3 of 3 people found this helpful
                                    • Re: Accessing GPIO and PWM on BeagleBone AI
                                      clem57

                                      And this makes perfect sense. The new Sitera for the BB-AI has pins at new places on the board versus the old Sitera.

                                      Clem

                                      • Re: Accessing GPIO and PWM on BeagleBone AI
                                        jomoenginer

                                        Again, your "node -pe "require('bonescript').bone.getPinObject('p8.22').ai.gpio"" command came from a Jason post regarding an issue with the node.js lib.

                                         

                                        The bonescript gave you 23 since P8_22 on a BB AI is mapped to gpio1_23 thus it is like ((32x0) +23) = gpio 23.

                                         

                                        Also, by default, the unused pins tend to be set to a default that seems to set it to the GPIO mode, mode 14. To set the pin to PWM, it would need to be set to Mode 10 to enable ehrpwm3B.

                                         

                                        Jason sent out a table but this is listed in the TI AM572x Data sheet as well:

                                        http://www.ti.com/lit/ds/symlink/am5728.pdf

                                         

                                        To make this change, from what I gather from Robert, the pin would need to be set to the following in the am5729-beagleboneai.dts

                                        Ex:

                                         DRA7XX_CORE_IOPAD(0x3794, PIN_OUTPUT | MUX_MODE10) /* AC8: P8.23: mmc3_dat4.ehrpwm3A - PWM3A */
                                        
                                        

                                         

                                        Changes to the .dts require a rebuild of the dtb:

                                         cd /opt/source/dtb-4.19-ti/
                                        make src/arm/am5729-beagleboneai.dtb 
                                        sudo make install
                                        sudo reboot 
                                        

                                         

                                         

                                        NOTE: The pin will not work as normal GPIO output at this point.

                                         

                                        Since the Adafruit stuff is not really working with the BB AI, I'm still looking for an example to test the PWM capability.

                                        2 of 2 people found this helpful
                                          • Re: Accessing GPIO and PWM on BeagleBone AI
                                            sjmill01

                                            Good stuff.

                                             

                                            When I tried altering the .dts earlier this week, it unfortunately bricked my SD card.  But, I had the 4.14x kernal I believe.  Did you have success with the .dts changes above with 4.19?

                                             

                                            I'll try again this evening once I get back from work and see if the BoneScript beta library will work with it.

                                             

                                            Thanks,

                                            Sean

                                              • Re: Accessing GPIO and PWM on BeagleBone AI
                                                jomoenginer

                                                It should work with either kernel, but I have only used 4.19.  I have done all of the latest updates and pulled in the latest code changes as well. 

                                                 

                                                After changing the pin mode, the analogInOut.js example no longer works so I suspect I am still missing something. It does sort of work with the pin as a GPIO, but only as an on or off state and does not fade the LED.

                                                 

                                                However, 4.19 seems to break the TIDL example where it will not build anymore and the blinkLED.js example causes the system to become nearly unresponsive.

                                                 

                                                Another thing I found with the Cloud9 examples is that you need to set the gpio port before running the scripts.

                                                Ex for P8_23 set as GPIO (Mode 14):

                                                echo 22 > /sys/class/gpio/export 
                                                echo out > /sys/class/gpio/gpio22/direction
                                                

                                                 

                                                I also removed the other led  except usr3 since I still want to see the heartbeat on the board.

                                                .

                                                const leds = ["USR3", "P8_23"];
                                                //const leds = ["USR0", "USR1", "USR2", "USR3", "P9_15"];
                                                

                                                 

                                                The board does not seem to become unresponsive after running the example this way.

                                                2 of 2 people found this helpful
                                                  • Re: Accessing GPIO and PWM on BeagleBone AI
                                                    sjmill01

                                                    okay...back at it again...

                                                     

                                                    I reverted back to 4.14 and got the classification.cpp to process again as you said.  It's much more responsive since they fixed the bug. It could use some training, but at least it processes fast and doesn't call my cat a ping pong ball anymore - although it did say coffee mug twice.

                                                     

                                                    At line 310, you can tap into the frame to do your own overlay.  Originally, it would put text on the frame if it just saw an object.  Below, I added an "else" so when it doesn't detect an object, the user gets validation that its at least trying:

                                                     

                                                     if(is_object >= 0)  //excerpt from cloud9/BeagleBone/AI/tidl/classification.cpp
                                                        {
                                                            cv::putText(
                                                                dst,
                                                                (*(labels_classes[is_object])).c_str(),
                                                                cv::Point(15, 60),
                                                                cv::FONT_HERSHEY_SIMPLEX,
                                                                1.5,
                                                                cv::Scalar(0,255,0),
                                                                3,  /* thickness */
                                                                8
                                                            );
                                                        } 
                                                        //new code is below that allows for a constant message or tweaking as you see fit.
                                                        else
                                                        {
                                                            char my_message[]="Searching...";
                                                            cv::putText(
                                                                dst,
                                                                my_message,
                                                                cv::Point(30, 60),
                                                                cv::FONT_HERSHEY_SIMPLEX,
                                                                1.5,
                                                                cv::Scalar(0,255,0),
                                                                3,  /* thickness */
                                                                8
                                                            );
                                                        }
                                                        //end of new code
                                                        
                                                    

                                                     

                                                     

                                                    Back to trying to get a pin to input.  I'm getting the following error with input even though I've set permissons on the files it thinks are missing:

                                                    ValueError: Set gpio mode failed, missing file or invalid permissions

                                                     

                                                    -Sean

                                                    1 of 1 people found this helpful
                                                • Re: Accessing GPIO and PWM on BeagleBone AI
                                                  sjmill01

                                                  Bad luck strikes again.  I followed this guide to edit the dts again:    https://www.elinux.org/EBC_Exercise_41_Pin_Muxing_for_the_AI

                                                   

                                                  The guide has you copy the dtb over versus do the sudo make install.  Don't know if there is a difference.

                                                   

                                                  Unfortunately, it led to another brick.  So, back to Etcher.  Might need to get another card to have one etching while testing running a trial on the other.

                                                   

                                                  -Sean

                                              • Re: Accessing GPIO and PWM on BeagleBone AI
                                                jomoenginer

                                                Thanks for posting what I had posted elsewhere.

                                                  • Re: Accessing GPIO and PWM on BeagleBone AI
                                                    sjmill01

                                                    If you're referring to the BeagleBoard forum, I'm on Jason's discussion as well, but it says my posts are awaiting the moderator to accept, I assume since I'm new to the forum.  You wouldn't have anything to drive PWM, would you?

                                                     

                                                    -Sean

                                                      • Re: Accessing GPIO and PWM on BeagleBone AI
                                                        jomoenginer

                                                        I'm referring to the following:

                                                        out = "P9_15"
                                                        
                                                        to
                                                        out = "P8_39"
                                                        

                                                         

                                                         

                                                        Perhaps it would be best if you reference where you find things.

                                                         

                                                        As I posted, the Adafruit BBIO.GPIO lib was written for the previous versions of the BBs and since the BB AI uses a AM57x which Jason noted has a different pin mapping, the Adafruit lib will not work as expected.  Mapping gpio76 between the BB Black and the BB AI:

                                                        BB Black - P9_15 - gpio1_16 -  GPIO - 48

                                                         

                                                        BB AI -     P9_15 - gpio3_12 - GPIO - 76

                                                        NOTE: As Jason stated, the AM57x does not have a gpio0, thus to calculate the GPIO value you skip gpio0 and come up with;

                                                          2 x 32 + 12 = 76

                                                         

                                                        I was calculating it as if it were a BB Black where this would have been 3 x 32 + 12 or 108.

                                                         

                                                        The Adafruit library is expecting P9_15 to be mapped to GPIO 48, to on a BB AI this does not work.   However, finding the GPIO 76 equivalent on the BB Black resulted in P8_39 or gpio2_12 (76).

                                                         

                                                        I filed an issue with the Adfruit BBIO.GPIO github repo to address this to see if there were any interest in getting it to properly support the BB AI.

                                                  • Re: Accessing GPIO and PWM on BeagleBone AI
                                                    sjmill01

                                                    Dude, I've been cobbling everything I can across a very quiet internet on this topic. Coming in cold to the BeagleBone platform, I've got a good 15 hours of research since I first posted this.  So, as soon as I find something that makes an LED blink, I zap it here so others don't have to lose the time concocting hacks to get their projects going.  I'm not taking credit for inventing the internet, just showing others how to get their LEDs to blink. Users here are always cool and helpful and I enjoy their conversation - e14 is my version of Facebook.

                                                     

                                                    Click "show earlier" and you will find links to every thread I'm monitoring bright and clear.  I hope others monitor them as well and translate them to simple solutions that a dumb guy like me can understand.

                                                     

                                                    -Sean

                                                    2 of 2 people found this helpful
                                                    • Re: Accessing GPIO and PWM on BeagleBone AI
                                                      sjmill01

                                                      Some work by Jason Kridner was just updated on github for BBAI GPIO and the UBoot process:

                                                       

                                                      https://github.com/beagleboard/beaglebone-ai/tree/master/SW

                                                       

                                                      when I run sudo apt update, it doesn't show anything to update.

                                                       

                                                      Anyone know how to integrate these changes (pasted below)?

                                                       

                                                      -Sean

                                                       

                                                      * Disable all UART TX pins (switch to GPIO)

                                                      * Disable SPI3 (switch to GPIO)

                                                      * Disable eHRPWMs (switch to GPIO)

                                                      * Match power-on pull-up/down status where possible

                                                      * Enable GPIOs on alternate pins (ie., other shorted pin is PRU input, UART RX, eQEP, etc.)

                                                      * Enable GPIOs for all pins not otherwise enabled

                                                      * On eQEP shorted pins, pull-down on eQEP and no-pull on GPIO

                                                      * On UART/PRU shorted pins, pull-down on GPIO and no-pull on UART/PRU

                                                      * Add pull-downs on UART-only pins (console)

                                                      * Correct voltages for vddshv1 and vddshv7 (3.3V)

                                                      1 of 1 people found this helpful
                                                      • Re: Accessing GPIO and PWM on BeagleBone AI
                                                        sjmill01

                                                        After two weeks and 20+ hrs of research, I'm calling this chicken done.  Here's the summary:

                                                         

                                                        BeagleBone AI Survival Guide V3.7: PWM, I2C, Analog/Digital Read/Write, Vision AI, Video Text Overlays, Audio, & Hardware

                                                        1 of 1 people found this helpful
                                                        • Re: Accessing GPIO and PWM on BeagleBone AI
                                                          stephensmith1211

                                                          Thanks for this information. It is useful