element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Atmel Xplained Boards
  • Products
  • Dev Tools
  • Atmel Xplained Boards
  • More
  • Cancel
Atmel Xplained Boards
Blog sama5d3 xplained violet - Using GPIO through bash
  • Blog
  • Forum
  • Documents
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Atmel Xplained Boards requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: violet
  • Date Created: 5 Oct 2014 8:36 PM Date Created
  • Views 143 views
  • Likes 1 like
  • Comments 3 comments
Related
Recommended

sama5d3 xplained violet - Using GPIO through bash

violet
violet
5 Oct 2014

This blog is about using some basic control methods to use the GPIO pins of the xplained board through bash commands, we'll look at using a digital pin as an output and then reading the value of it as an input pin.

 

I already wrote a quick post about pin control in the main roadtest comments. The user tekmeister pointed out that this is a standard way of pin control on other embedded linux platforms like Raspberry Pi which is great news! It follows that other things we would like to do in the future could be a lot easier by searching for guides based around things like the Pi or Beaglebone and modifying them a little to use on the Xplained! Sounds like a good way to get a foothold.

 

We start off at the Xplained command prompt, you can get there by using the quickstart guide and connecting through usb or following my blog on connecting through SSH.

 

Get a directory listing of /sys/class/gpio

 

type:

ls /sys/class/gpio

 

You should see the following directory listing

export    gpiochip0  gpiochip128    gpiochip32  gpiochip64    gpiochip96  unexport

 

next we need to export the pin we want to use into the filesystem

 

type:

echo 81 > /sys/class/gpio/export

 

The number 81 is the assignment number of pin C17 which is located on the digital header on the xplained board. If we do another directory listing of /sys/class/gpio we will see a new directory called pioC17

type:

ls /sys/class/gpio

 

You should see the same directory listing as before only a new directory called pioC17 has been created.

export   gpiochip128    gpiochip64  pioC17

gpiochip0  gpiochip32    gpiochip96  unexport

 

 

 

If we get a directory listing on pioC17 we will see the basic control structure of that pin.

type:

 

ls /sys/class/gpio/pioC17

 

You should see the following directory listing:

active_low  device  direction  edge  power  subsystem  uevent  value

 

Its easy to see that some of these are properties of the way we would use the pin. "direction" relates to whether the pin is in input or output state and the "value" relates to whether it is high or low (0 or 1) by reading it when it is input state or setting the value if we are using it as an output. we'll use "cat" to read the files and "echo" to modify them to set the pins state.

type:

cat /sys/class/gpio/pioC17/direction

 

You should see the following

root@sama5d3_xplained:~# cat /sys/class/gpio/pioC17/direction

in

root@sama5d3_xplained:~#

 

the word "in" that comes after you read the file tells us that it is set as an input. if we type cat /sys/class/gpio/pioC17/value we will get the number 1 that tells us that the pin is high, presumably through an internal pull up resistor, if we physically connect pin C17 to a suitable ground on the board, and type cat /sys/class/gpio/pioC17/value again, we will get the number 0 because the pin is now in a low state.

 

That shows how we can receive input through the digital header, next we will look at using it is an output

type:

echo out > /sys/class/gpio/pioC17/direction

 

This will write the value "out" to the direction file. If we do cat /sys/class/gpio/pioC17/direction you should see the word "out" instead of "in" this time. If we do cat /sys/class/gpio/pioC17/value we will get "0" as the outcome, this tells us that the pin is low. If you connect a multimeter to ground and pin C17 on the digital header, it will show 0v. Type "echo 1 > /sys/class/gpio/pioC17/value" and you will notice the multimeter jumps to 3.3V.

 

Great! We can turn the pin on and off physically do connect to an H-Bridge or LED etc.. and in the words of Ben Heck "If we can turn on a LED we can do anything!"

 

My next blog is going to be using what we know about pin control and using it in Python in order to make it more useful and let us write small more complex scripts and be able to automate what we want the pins doing.

 

As an offshoot to my roadtest project, I'm going to be making a small GPIO control library in Python, there is already one for the Raspberry Pi that might work but the library is compiled in C and possibly wouldn't be using the same C library structure since the Xplained board is using uclibc. Feel free to try, but I'm eager to get going and try to make interrupts that run full scripts or call functions.

Anonymous
Parents
  • sjcchuma
    sjcchuma over 7 years ago

    Hi, not sure if this good place to ask but i am trying to gain access to BP3 (PB_USER) button. I came up with GPIO number 61 (PE29), is this correct? After configuration below, I got pioB29 and press button didn't change value. Could you help?

     

    root@SAMA5D3-Xplained:/sys/class/gpio# echo 61 > export

    root@SAMA5D3-Xplained:/sys/class/gpio# ls

    export     gpiochip128  gpiochip64  pioB29
    gpiochip0  gpiochip32   gpiochip96  unexport

    root@SAMA5D3-Xplained:/sys/class/gpio# cd pioB29

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# ls

    active_low  device  direction  edge  power  subsystem  uevent  value

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# echo out > direction

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# echo 1 > value

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# cat value

    1

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# cat value

    1

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# uname -a

    Linux SAMA5D3-Xplained 3.16.5-sama5-armv7-r8 #1 Mon Oct 27 11:01:55 PDT 2014 armv7l GNU/Linux

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Comment
  • sjcchuma
    sjcchuma over 7 years ago

    Hi, not sure if this good place to ask but i am trying to gain access to BP3 (PB_USER) button. I came up with GPIO number 61 (PE29), is this correct? After configuration below, I got pioB29 and press button didn't change value. Could you help?

     

    root@SAMA5D3-Xplained:/sys/class/gpio# echo 61 > export

    root@SAMA5D3-Xplained:/sys/class/gpio# ls

    export     gpiochip128  gpiochip64  pioB29
    gpiochip0  gpiochip32   gpiochip96  unexport

    root@SAMA5D3-Xplained:/sys/class/gpio# cd pioB29

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# ls

    active_low  device  direction  edge  power  subsystem  uevent  value

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# echo out > direction

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# echo 1 > value

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# cat value

    1

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# cat value

    1

    root@SAMA5D3-Xplained:/sys/class/gpio/pioB29# uname -a

    Linux SAMA5D3-Xplained 3.16.5-sama5-armv7-r8 #1 Mon Oct 27 11:01:55 PDT 2014 armv7l GNU/Linux

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Children
  • violet
    violet over 7 years ago in reply to sjcchuma

    hey,

     

    I tracked the user button down to being assigned to gpio.keys.4 I think, sorry not with the boards right now but it definately corresponds to the keys.4 part, You can set os level interrupts etc.. with it that way. I think you'd have to unassign it bfore you can use it for gpio. There may well be some other control structure in place that you can use to interface with it.

     

    Ive been working on changing the functions of pins too but not had a great deal of luck. The click shield we were sent for the road test would use pins A4 and A5 as a bit banged I2c interface, The SHT1 module doesnt work with normal I2c pins so you cant just bridge connect it and use the I2c modules. Those pins are registered as being on the LCD.1 bus and won't work as GPIO. The user guide shows their function as Analog only pins with no reference to GPIO. The only way Ive been able to user it is to bridge extra wire across the board from other GPIO but obviously the click shield "should" be plug and play without having to do bridge mods... Battling on lol! xx

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • sjcchuma
    sjcchuma over 7 years ago in reply to violet

    Thanks for quick reply.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Element14

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube