Raspberry Pi_Kor_25.6.2 WiringPi library


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


25.6.2  <WiringPi> library

 

25.6.2.1    <WiringPi> library 개요

 

<WiringPi> library Gordon Henderson이 개발한 GPIO 도구이다. 이것은 Raspberry Pi에서 사용되는 BCM2835에서 사용하기 위해 C 언어로 개발된 GPIO 처리 library로서 GNU LGPLv3 라이선스 조건으로 배포되고 있다. library는 원래 Arduino의 “wiring system에 익숙한 사용자들이 Raspberry Pi를 쉽게 사용할 수 있도록 하기 위해서 개발되었다.

 

library C C++뿐만 아니라, 적합한 wrapper를 사용하여 Python, Java, Perl 등 다른 개발언어에서도 사용할 수 있다.

 

<WiringPi> library에는 command-line에서 사용할 수 있는 gpio 명령이 포함되어 있는데, 이 명령으로 GPIO pin을 원하는 방식으로 작동하도록 프로그래밍을 하거나 필요한 준비작업을 할 수 있다. 또 이 명령을 사용하여 pin에서 신호입력을 읽거나 출력신호를 보낼 수 있으며, Shell script에서 GPIO pin들을 통제할 수 있다.

 

<WiringPi> library는 확장이 가능한 도구인데, Gertboard에 있는 analog interface 장치를 사용하거나, MCP23x17 / MCP23x08 (I2C 7 SPI) GPIO expansion chip을 사용할 수 있도록 <WiringPi> library를 확장해주는 module이 제공된다. 또한 Raspberry Pi serial port를 통하여 ATmega (e.g. Arduino, or the Gertboard) 장치를 추가적인 GPIO expansion으로 사용할 수 있도록 처리해주는 모듈도 제공해 준다. 이 외에도 사용자들이 자신의 주변장치와 연동하기 위한 자체적인 확장모듈을 개발할 때도 <WiringPi> library를 유용하게 이용할 수 있다.

 

<WiringPi> library analog 입력/출력을 지원하지만, Raspberry Pi에는 analog H/W가 없기 때문에 Gertboards analog chip이나 다른 analog/digital 장치, digital/analog 장치가 비교적 쉽게 설치될 수 있도록 지원하는 모듈이 제공된다.

 

상세한 내용은 다음을 참고하기 바란다.    

    http://wiringpi.com

 

 


 

25.6.2.2    <WiringPi> library pin map

 

<WiringPi> library에서 사용하는 GPIO pin 번호는 원래의 Raspberry Pi GPIO pin 번호와 서로 다르다. 아래는 서로 간의 mapping 현황과 GPIO pin별 기본적인 상태를 보여주는 도표이다.

 

Mode

V

Name

Wiring Pi

No

BCM

No

Physical

No

BCM

No

Wiring Pi

No

Name

V

Mode

3.3v

 

 

1

2

 

 

5v

ALT0

1

SDA.1

8

2

3

4

 

 

5v

ALT0

1

SCL.1

9

3

5

6

 

 

0v

IN

0

GPIO. 7

7

4

7

8

14

15

TxD

1

ALT0

0v

 

 

9

10

15

16

RxD

1

ALT0

IN

0

GPIO. 0

0

17

11

12

18

1

GPIO. 1

0

IN

IN

0

GPIO. 2

2

27

13

14

 

 

0v

IN

0

GPIO. 3

3

22

15

16

23

4

GPIO. 4

0

IN

3.3v

 

 

17

18

24

5

GPIO. 5

0

IN

ALT0

0

MOSI

12

10

19

20

 

 

0v

ALT0

0

MISO

13

9

21

22

25

6

GPIO. 6

0

IN

ALT0

0

SCLK

14

11

23

24

8

10

CE0

1

ALT0

0v

 

 

25

26

7

11

CE1

1

ALT0

IN

0

SDA.0

30

0

27

28

1

31

SCL.0

0

IN

IN

0

GPIO.21

21

5

29

30

 

 

0v

IN

0

GPIO.22

22

6

31

32

12

26

GPIO.26

0

IN

IN

0

GPIO.23

23

13

33

34

 

 

0v

IN

0

GPIO.24

24

19

35

36

16

27

GPIO.27

1

OUT

IN

0

GPIO.25

25

26

37

38

20

28

GPIO.28

0

IN

0v

 

 

39

40

21

29

GPIO.29

0

IN

25‑4 <WiringPi> library pin map


 

제목에 있는 각 항목의 의미는 다음과 같다

    Mode         -- pin의 사용 상태

IN              -- 입력

OUT           -- 출력

ALTn           -- pin에 대한 ALTernative mode - ALT0 ~ ALT5

 

Raspberry Pin layout에 대한 보다 상세한 자료는 다음 자료를 참조하기 바란다.

    http://pi.gadgetoid.com/pinout/pin31_gpio6

 


 

25.6.2.3    <WiringPi> library 설치

 

<WiringPi> library GitHub에서 프로그램을 download해서 설치해야 한다. GitHub에서 자료를 download하기 위해서는 소스관리 툴인 git-core를 설치해야 한다. 이에 대해서는 [17.4.3 GitHub를 이용해서 인터넷에서 download하는 방법]을 참고하기 바란다.

 

먼저, 다음과 같은 "git" 명령으로 <WiringPi> Library download한다.

 

git clone  git://git.drogon.net/WiringPi

 

 

그런 다음, <WiringPi> library download되어 있는 <wiringPi> directory로 이동하여 내부에 포함되어 있는 "build" script를 수행한다.  

 

pi@raspberrypi ~/wiringPi $ ./build

wiringPi Build script

=====================

WiringPi Library

[UnInstall]

[Compile] wiringPi.c

[Compile] wiringSerial.c

~ 중략

~ 중략

 

All Done.

NOTE: To compile programs with wiringPi, you need to add:

    -lwiringPi

  to your compile line(s) To use the Gertboard, MaxDetect, etc.

  code (the devLib), you need to also add:

    -lwiringPiDev

  to your compile line(s).

 


 

설치가 잘 되었는지 확인하기 위해서는 다음의 명령을 실행하여 정상적으로 처리되는지 확인한다.

 

pi@raspberrypi ~/wiringPi $ gpio -v

gpio version: 2.26

Copyright (c) 2012-2015 Gordon Henderson

This is free software with ABSOLUTELY NO WARRANTY.

For details type: gpio -warranty

 

Raspberry Pi Details:

  Type: Model B+, Revision: 1.2, Memory: 512MB, Maker: Sony


 

25.6.2.4    "gpio" 명령 

 

<WiringPi> library에는 command-line에서 사용할 수 있는 "gpio" 명령이 포함되어 있는데, 이 명령으로 GPIO pin을 원하는 방식으로 작동하도록 프로그래밍을 하거나 필요한 준비작업을 할 수 있다. 또 이 명령을 사용하여 pin에서 신호입력을 읽거나  출력신호를 pin으로 보낼 수 있으며, Shell script에서 GPIO pin들을 통제할 수 있다.

 

[명령 형식]

gpio   [option]

 

[명령 개요]                               

    Raspberry Pi에 설치되어 있는 GPIO pin을 통제하거나 GPIO pin에서 입력신호를 받거나 GPIO pin으로 출력신호를 받을 수 있도록 해주는 도구이다.

    user 권한    -- 일반 user.

 

[상세 설명]

    이 명령은 Raspberry Pi에 있는 GPIO pin이나 Gertboard에 있는 SPI A/D D/A 변환기에 손쉽게 접근하도록 해주는 명령 도구이다.

    이 명령으로 PiFace IO board에서 IO를 통제할 수 있고, SPI I2C kernel module load할 수 있다.

    이것은 기본적으로 간단한 test와 원인 분석 용도로 만들어졌지만, GPIO 조정 속도가 조금 느려도 된다면 Shell script에서 일반적인 목적으로 사용할 수도 있다.

 

[주요 option]

-v

Output the current version including the board revision of the Raspberry Pi.

-g

Use the BCM_GPIO pins numbers rather than wiringPi pin numbers. Note: The BCM_GPIO pin numbers are always used with the export and edge commands.

-1

Use the physical pin numbers rather than wiringPi pin numbers. Note: that this applies to the P1 connector only. It is not possible to use pins on the Revision 2 P5 connector this way, and as with -g the BCM_GPIO pin numbers are always used with the export and edge commands.

-x extension

 

This causes the named extension to be initialised. Extensions comprise of a name (e.g. mcp23017) followed by a colon, then the pin-base, then more optional parameters depending on the extension type.  See the web page on http://wiringpi.com/the-gpio-utility/

-p

Use the PiFace interface board and its corresponding pin numbers. The PiFace will always appear at pin number 200 in the gpio command. You can assign any pin numbers you like in your own programs though.

read <pin>

Read the digital value of the given pin and print 0 or 1 to represent the respective logic levels.

write <pin> <value>

Write the given value (0 or 1) to the pin. You need to set the pin to output mode first.

readall

 

Output a table of all GPIO pins values. The values represent the actual values read if the pin is in input mode, or the last value written if the pin is in output mode. The readall command is usable with an extension module (via the -x parameter), but it's unable to determine pin modes or states, so will perform both a digital and analog read on each pin in-turn.

pwm <pin> <value>

 

Write a PWM value (0-1023) to the given pin. The pin needs to be put into PWM mode first.

clock <pin> <frequency>

Set the output frequency on the given pin. The pin needs to be put into clock mode first.

mode <pin> <mode>

 

Set a pin into input, output or pwm mode. Can also use the literals up, down or tri to set the internal pull-up, pull-down or tristate (off) controls.

The ALT modes can also be set using alt0, alt1, ... alt5.

 


 

[사용 Example]

다음은 GPIO pin들의 현재 상태를 보여준다. GPIO pin들의 배열 모양대로 좌/우로 각각의 pin들에 대한 상태를 보여주고 있다.

 

pi@raspberrypi ~ $ gpio readall

+-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+

 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |

 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+

 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |

 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |

 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |

 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |

 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |

 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 1 | IN   | GPIO. 1 | 1   | 18  |

 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |

 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |

 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |

 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |

 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |

 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |

 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |

 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |

 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |

 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |

 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |

 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |

 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |

 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |

 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+

 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |

 +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+


 

25.6.2.5    <WiringPi> library 기본 사용법

 

다음은 C 언어를 기준으로 <WiringPi> library 의 간단한 사용법을 설명한 것이다. 다른 언어에 대해서는 관련 자료를 참조하기 바란다.

 

 

   port 번호

 

port 번호에는 특별한 지정이 없는 한 <WiringPi> library 기준의 port 번호를 사용해야 한다.

 

 

   include

 

GPIO 라이브러리를 사용하려면 header include를 해야 한다. 프로그램의 처음에서 처리한다.

 

#include <wiringPi.h>

 

 

   Setup

 

<WiringPi> library는 사용하기 전에 먼저 초기화를 해주어야 한다.

 

wiringPiSetup ()

 

 

   pin Mode 설정

 

GPIO 포트들은 사용하기 전에 먼저 사용용도를 입력 또는 출력으로 설정을 해주어야 한다.

 

pinMode (<port> , <pin-mode> )

 

 

 

pin-mode는 해당 port를 입력 또는 출력으로 사용할 것인지를 지정하는 것이다.

    INPUT         -- 입력

    OUTPUT      -- 출력

 

Example) pinMode (0, OUTPUT) ;  

 

 

   Output

 

출력 port로 지정된 port에 대해서는 지정된 값을 출력할 수 있다.

 

digitalWrite (<port> , <output> )

 

output는 다음과 같은 의미를 가진다.

    HIGH         -- 전류 On

    LOW           -- 전류 Off

 

Example)digitalWrite (0, HIGH)

 

 

   Input

 

입력 port로 지정된 port에 대해서는 port에서 들어오는 값을 읽을 수 있다.

 

variable = digitalRead ( <port> )

 

intput는 다음과 같은 의미를 가진다

    HIGH          -- 전류 On

    LOW           -- 전류 Off

 

Example) variable = digitalRead ( 0 )

 

<WiringPi> library를 이용한 interface 처리에 대한 사례는 [25.7.1.2<WiringPi> library를 이용한 사례]을 참고하기 바란다.

Leave a Reply