This is the third part of Linux tutorial for Linux using the ZYBO board from Digilent.
- ZYBO-Linux(I): Escribir la imagen de Linux-Debian en una SD
- ZYBO-Linux(II): Install and start up Linux from ZYBO
- ZYBO-Linux(III): Conectar con la ZYBO a través del terminal de Ubuntu
In this part I am going to describe the steps to start up Linux from the previously c burnt SD card. The communication with the board are hold via Ethernet. At the end some easy programs will be shown to handle with the Zybo Leds.
0- You have to burn a SD card with the Debian image. Done in the Linux-Debian step (I). Also remember to change the Jumper JP5 to the SD position.
1- Connect the ZYBO board via Ethernet to the computer. If your laptop does not have an Ethernet port, you would need an USB adapter like in the picture below.
2- Configure your connection in the net configuration and select Ethernet or wired connection.
Introduce the following address in the IPv4 and IPv6 settings, since the preloaded image have this as default:
You must ignore the Ipv6 connection
3- Open the terminal in a Linux computer (Ubuntu, Debian, etc). We did this on the previous step. Then connect via ssh (secure sehll) with the board. Therefore the following command should be written on the terminal:
ssh root@192.168.1.120
The username and pass should be root root
The address which come by default is 192.168.1.120. This can be changed in another images or configurations.
4- To access to the root folder graphically, to see and modify their content. Open a any folder (Nautilus) and then click on “connect to Server” as the following image is showing:
Introduce the IP of the previous Ethernet port:
Write “sftp://192.168.1.120” and then press connect. After that you hould be able to see the content of the root folder.
This can be done in Debian with the command
caja sftp://192.168.1.120
5- Create a new text document with the root folder of the SD by right click on the folder and then selecting new file. Then you can rename the file as you want, for example blink.c
7- Write the code inside the text file editing it with the normal text editor. Just double click on the icon and the notebloc will open.
Userspace applications are not allowed to access to direct to the hardware (in linux and in every OS in general), and therefore the led or buttons. While using an operating system like linux in this case, the linux kernel act as an interface.
We are going to use the easiest GPIO driver: Sysfs Interface (Read the official linux doc for more info). We will see more details after this example.
Write your code, or copy and modify the following example code. This small program blink the MIO-7 led built on Zybo: Download this other blink-example.c.
#include #include #include /* Blinking led example www.misCircuitos.com*/ int main () { int exportfd = open( "/sys/class/gpio/export" , O_WRONLY ); if( exportfd < 0){ printf( "Cannot open GPIO to export it n"); exit (1); } write( exportfd ,"913",4); close( exportfd ); printf(" GPIO exported successfully n " ); /* Get the GPIO value ready to be toggled */ int valuefd = open( "/sys/class/gpio/gpio913/value" , O_RDWR ); if ( valuefd < 0){ printf(" Cannot open GPIO value n " ); exit (1); } printf(" GPIO value opened , now toggling ...n " ); /* toggle the GPIO forever , press control + c to stop it */ while (1) { int i ; for(i=0;i<0xFFFFFF;i++){} //Software delay write( valuefd , "1" , 2); for(i=0;i<0xFFFFFF;i++){} //Software delay write( valuefd , "0" , 2); } }
In the text editor it should appear like this:
7- Save and compile the program. Therefore we are going to use the preinstalled GNU gcc compiler of Linux. For that the structure is:
gcc C-SOURCE-FILENAME -o OUTPUT-FILENAME
Another useful options for the compiler:
- –help
- -g (build with debugging symbols)
- -O0 (turn off optimization)
- -O2 (full optimizations)
- -S (generate assembly output instead)
- -Wall (turn on all compiler warnings)
In this after opening a terminal, the following command is written:
gcc blink -o blink
Now, if the compilation has run without errors, you should see an empty like as shown in the following picture:
In the case of any mistake or compilation error are in the code, the compiler shows you the error and tell you in which line the error can be found. For example something like this:
8- Now, it is time to execute our program, of course only if it has compiled without errors! Therefore, the next command will run the code:
./blink
After that the led should blink.
Important note: to stop the program, you should press Control + C
sysfs GPIO driver: Code Examples in C
The sysfs drivers files and documentation can be seen on the linux github.
The procedure to use the external GPIO (general purpose input output):
- First the GPIO has to be exported writing the number of the pin inside the text file on the directory: /sys/class/gpio/export (in this case 913 for the LED MIO07)
- Later the direction has to be written into the file /sys/class/gpio/gpio913/direction (“out” or “in”)
- To write the value into the GPIO pin, a value “1” or “0” should be written in the text file on the directory: /sys/class/gpio/gpio913/value.
For the ZYBO board, the offset value the base address of the GPIO is 906. So, manage the pin of the PS (processor System) connected to MIO 7 (led) you have to set the 906+7 = 913. For the buttons MIO 50 and MIO 51–> 906+50 = 956 and 906+ 51 = 957
The processor system is connected to the Pmod JF (MIO)
Example for configuring the ps-buttons (MIO50-51), pins(MIO-13 JF1) and led (MIO-7): Download the c-file gpio_example.c
#include #include #include int main () { int exportfd; int valuefd_led, value_pin, valuefd_but1, valuefd_but0; int directionfd_led, directionfd_pin, directionfd_but0, directionfd_but1; char read_value0, read_value1 ; int counter = 0; //Export GPIO ports exportfd = open ( "/sys/class/gpio/export" , O_WRONLY ); if ( exportfd < 0) { printf ( "Cannot open GPIO to export it \n"); exit (1); } write ( exportfd , "913" , 4); // base_address + led_addres. 906+7 = 913 write ( exportfd , "919" , 4); // base_address + led_addres. 906+13 = 919 write ( exportfd , "956" , 4); // base_address + button_addres. 906+50 = 956 write ( exportfd , "957" , 4); // base_address + button_addres. 906+51 = 957 close ( exportfd ); printf ( "GPIO exported successfully \n " ); // Set the direction of the GPIO as an output or input directionfd_led = open ( "/sys/class/gpio/gpio913/direction" , O_RDWR ); directionfd_pin = open ( "/sys/class/gpio/gpio919/direction" , O_RDWR ); directionfd_but0 = open ( "/sys/class/gpio/gpio956/direction" , O_RDWR ); directionfd_but1 = open ( "/sys/class/gpio/gpio957/direction" , O_RDWR ); if ( directionfd_led < 0) { printf ( " Cannot open GPIO direction it \n " ); exit (1); } write ( directionfd_led , "out" , 4); close ( directionfd_led ); if ( directionfd_pin < 0) { printf ( " Cannot open GPIO direction it \n " ); exit (1); } write ( directionfd_pin , "in" , 3); close ( directionfd_pin); if ( directionfd_but0 < 0) { printf ( " Cannot open GPIO direction it \n " ); exit (1); } write ( directionfd_but0 , "in" , 3); close ( directionfd_but0); if ( directionfd_but1 < 0) { printf ( " Cannot open GPIO direction it \n " ); exit (1); } write ( directionfd_but1 , "in" , 3); close ( directionfd_but1 ); printf ( " GPIO direction set to led, pin JF1, button 0 and button 1 as output successfully \n" ); /* Get the GPIO handler */ valuefd_led = open( "/sys/class/gpio/gpio913/value" , O_RDWR ); value_pin = open( "/sys/class/gpio/gpio919/value" , O_RDWR ); valuefd_but0 = open( "/sys/class/gpio/gpio956/value" , O_RDWR ); valuefd_but1 = open( "/sys/class/gpio/gpio957/value" , O_RDWR ); if ( valuefd_led < 0) { printf( "Cannot open GPIO value \n " ); exit (1); } if ( value_pin < 0) { printf( "Cannot open GPIO value 957\n " ); exit (1); } if ( valuefd_but0 < 0) { printf( "Cannot open GPIO value 956 \n " ); exit (1); } if ( valuefd_but1 < 0) { printf( "Cannot open GPIO value 957\n " ); exit (1); } while (1) { write(valuefd_led, "1",2); //Write the led value write(valuefd_pin, "1",2); //Write the pin value //poll button status lseek(valuefd_but0 ,0,SEEK_SET); read(valuefd_but0, &read_value0,1); lseek(valuefd_but1 ,0,SEEK_SET); read(valuefd_but1, &read_value1,1); printf("but_0 = %d but_1 = %d \n", read_value0, read_value1); //Buttons status } }
A simply pwm generated by software:
/////////////////////////////////
/* Simple pwm per software */
/* Alberto Lopez December 2017 */
/* misCircuitos.com */
/////////////////////////////////
#include
#include
#include
int main( int argc, char *argv[] )
{
int exportfd;
const PERIODE = 1000; //periode in us
int delay_low = 100; //high cycle in microseconds
int delay_high = PERIODE - delay_low;
//Export GPIO
exportfd = open( "/sys/class/gpio/export" , O_WRONLY );
if( exportfd < 0){
printf( "Cannot open GPIO to export it \n");
exit (1);
}
write( exportfd ,"913",4); //MIO-7 LED
write( exportfd ,"919",4); //MIO-13 PIN-out
close( exportfd );
printf(" GPIO exported successfully \n " );
//Direction
int directionfd = open("/sys/class/gpio/gpio913/direction", O_RDWR);
if( directionfd < 0){
printf("cannot open the file");
exit(1);
}
write( directionfd , "out" , 4);
close (directionfd);
int directionfdO = open("/sys/class/gpio/gpio919/direction", O_RDWR);
if( directionfdO < 0){
printf("cannot open the file");
exit(1);
}
write( directionfdO , "out" , 4);
close (directionfdO);
// Get the GPIO value ready to be toggled */
int valuefd = open( "/sys/class/gpio/gpio913/value" , O_RDWR );
int valuefdO = open( "/sys/class/gpio/gpio919/value" , O_RDWR );
if ( valuefd < 0){
printf(" Cannot open GPIO value \n " );
exit (1);
}
printf(" GPIO value opened , now toggling ...\n " );
/* toggle the GPIO forever , press control + c to stop it */
while (1)
{
int i ;
printf(" GPI0 -off\n");
write( valuefd , "1" , 2);
write( valuefdO , "1" , 2);
usleep(delay_low);
printf(" GPI0 -on\n");
write( valuefd , "0" , 2);
write( valuefdO , "0" , 2);
usleep(delay_high);
}
}
xxx
Appreciate the recommendation. Let me try it out.
Nahrenako
Thanks for sharing this instruction, please help to answer this question:
After ssh root@192.168.1.120 I’ve been asked for password, what password is?
Alberto L.
It should be ‘root’ or ‘admin’ (if I remember well)
Barbary
I keep trying all different password combinations to connect to the zybo-board, but they’re not working. Is there something that I’m missing here?
Barbary
Edit: Actually, “root” gives me permission denied.