Raspberry Pi 2 및 3 핀 매핑

Raspberry Pi 2 & 3 Pin Header

Raspberry Pi 2 및 Raspberry Pi 3용 하드웨어 인터페이스는 보드의 40핀 헤더 J8 을 통해 노출됩니다. 기능에는 다음이 포함됩니다.

  • 24x - GPIO 핀
  • 1x - 직렬 UART(RPi3에는 미니 UART만 포함)
  • 2x - SPI 버스
  • 1x - I2C 버스
  • 2x - 5V 전원 핀
  • 2x - 3.3V 전원 핀
  • 8x - 접지 핀

GPIO 핀

이 디바이스에서 사용할 수 있는 GPIO를 살펴보겠습니다.

GPIO 핀 개요

API를 통해 액세스할 수 있는 GPIO 핀은 다음과 같습니다.

Gpio# 전원 켜기 풀 대체 함수 머리글 핀
2 풀업 I2C1 SDA 3
3 풀업 I2C1 SCL 5
4 풀업 7
5 풀업 29
6 풀업 31
7 풀업 SPI0 CS1 26
8 풀업 SPI0 CS0 24
9 풀다운을 SPI0 MISO 21
10 풀다운을 SPI0 MOSI 19
11 풀다운을 SPI0 SCLK 23
12 풀다운을 32
13 풀다운을 33
16 풀다운을 SPI1 CS0 36
17 풀다운을 11
18 풀다운을 12
19 풀다운을 SPI1 MISO 35
20 풀다운을 SPI1 MOSI 38
21 풀다운을 SPI1 SCLK 40
22 풀다운을 15
23 풀다운을 16
24 풀다운을 18
25 풀다운을 22
26 풀다운을 37
27 풀다운을 13
35* 풀업 레드 파워 LED
47* 풀업 녹색 활동 LED

* = Raspberry Pi 2만. GPIO 35 및 47은 Raspberry Pi 3에서 사용할 수 없습니다.

GPIO 샘플

예를 들어 다음 코드는 GPIO 5를 출력으로 열고 핀에 디지털 '1'을 씁니다.

using Windows.Devices.Gpio;

public void GPIO()
{
    // Get the default GPIO controller on the system
    GpioController gpio = GpioController.GetDefault();
    if (gpio == null)
        return; // GPIO not available on this system

    // Open GPIO 5
    using (GpioPin pin = gpio.OpenPin(5))
    {
        // Latch HIGH value first. This ensures a default value when the pin is set as output
        pin.Write(GpioPinValue.High);

        // Set the IO direction as output
        pin.SetDriveMode(GpioPinDriveMode.Output);

    } // Close pin - will revert to its power-on state
}

핀을 열면 풀 저항기가 포함될 수 있는 전원 켜기 상태가 됩니다. 끌어오기 저항기의 연결을 끊고 높은 임피든스 입력을 가져오려면 드라이브 모드를 GpioPinDriveMode.Input으로 설정합니다.

    pin.SetDriveMode(GpioPinDriveMode.Input);

핀을 닫으면 전원 켜기 상태로 되돌리기.

Muxing 고정

일부 GPIO 핀은 여러 함수를 수행할 수 있습니다. 기본적으로 핀은 GPIO 입력으로 구성됩니다. 호출하거나 SpiDevice.FromIdAsync() 호출 I2cDevice.FromIdAsync() 하여 대체 함수를 열면 함수에 필요한 핀이 자동으로 올바른 함수로 전환됩니다("muxed"). 디바이스를 호출 I2cDevice.Dispose() 하여 닫거나 SpiDevice.Dispose()핀이 기본 함수로 다시 되돌리기. 서로 다른 두 함수에 핀을 한 번에 사용하려고 하면 충돌하는 함수를 열려고 할 때 예외가 throw됩니다. 예를 들면 다음과 같습니다.

var controller = GpioController.GetDefault();
var gpio2 = controller.OpenPin(2);      // open GPIO2, shared with I2C1 SDA

var dis = await DeviceInformation.FindAllAsync(I2cDevice.GetDeviceSelector());
var i2cDevice = await I2cDevice.FromIdAsync(dis[0].Id, new I2cConnectionSettings(0x55)); // exception thrown because GPIO2 is open

gpio2.Dispose(); // close GPIO2
var i2cDevice = await I2cDevice.FromIdAsync(dis[0].Id, new I2cConnectionSettings(0x55)); // succeeds because gpio2 is now available

var gpio2 = controller.OpenPin(2); // throws exception because GPIO2 is in use as SDA1

i2cDevice.Dispose(); // release I2C device
var gpio2 = controller.OpenPin(2); // succeeds now that GPIO2 is available

직렬 UART

RPi2/3 에서 사용할 수 있는 직렬 UART가 하나 있습니다. UART0

  • 핀 8 - UART0 TX
  • 핀 10 - UART0 RX

아래 예제에서는 UART0초기화하고 쓰기를 수행한 다음 읽기를 수행합니다.

using Windows.Storage.Streams;
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;

public async void Serial()
{
    string aqs = SerialDevice.GetDeviceSelector("UART0");                   /* Find the selector string for the serial device   */
    var dis = await DeviceInformation.FindAllAsync(aqs);                    /* Find the serial device with our selector string  */
    SerialDevice SerialPort = await SerialDevice.FromIdAsync(dis[0].Id);    /* Create an serial device with our selected device */

    /* Configure serial settings */
    SerialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
    SerialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
    SerialPort.BaudRate = 9600;                                             /* mini UART: only standard baud rates */
    SerialPort.Parity = SerialParity.None;                                  /* mini UART: no parities */  
    SerialPort.StopBits = SerialStopBitCount.One;                           /* mini UART: 1 stop bit */
    SerialPort.DataBits = 8;

    /* Write a string out over serial */
    string txBuffer = "Hello Serial";
    DataWriter dataWriter = new DataWriter();
    dataWriter.WriteString(txBuffer);
    uint bytesWritten = await SerialPort.OutputStream.WriteAsync(dataWriter.DetachBuffer());

    /* Read data in from the serial port */
    const uint maxReadLength = 1024;
    DataReader dataReader = new DataReader(SerialPort.InputStream);
    uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
    string rxBuffer = dataReader.ReadString(bytesToRead);
}

직렬 UART 코드를 실행하려면 UWP 프로젝트의 Package.appxmanifest 파일에 다음 기능을 추가해야 합니다.

Visual Studio 2017에는 serialcommunication 기능에 영향을 주는 매니페스트 디자이너(appxmanifest 파일의 시각적 편집기)에 알려진 버그가 있습니다. appxmanifest가 serialcommunication 기능을 추가하는 경우 디자이너를 사용하여 appxmanifest를 수정하면 appxmanifest가 손상됩니다(Device xml 자식이 손실됨). appxmanifest를 마우스 오른쪽 단추로 클릭하고 상황에 맞는 메뉴에서 코드 보기를 선택하여 appxmanifest를 직접 편집하여 이 문제를 해결할 수 있습니다.

  <Capabilities>
    <DeviceCapability Name="serialcommunication">
      <Device Id="any">
        <Function Type="name:serialPort" />
      </Device>
    </DeviceCapability>
  </Capabilities>

I2C Bus

이 디바이스에서 사용할 수 있는 I2C 버스를 살펴보겠습니다.

I2C 개요

핀 헤더에는 두 줄 SDASCL이 있는 하나의 I2C 컨트롤러 I2C1이 노출됩니다. 1.8KΜS 내부 풀업 저항기는 이미 이 버스용 보드에 설치되어 있습니다.

신호 이름 머리글 고정 번호 Gpio 번호
SDA 3 2
SCL 5 3

아래 예제에서는 I2C1초기화하고 주소 0x40 있는 I2C 디바이스에 데이터를 씁니다.

using Windows.Devices.Enumeration;
using Windows.Devices.I2c;

public async void I2C()
{
    // 0x40 is the I2C device address
    var settings = new I2cConnectionSettings(0x40);
    // FastMode = 400KHz
    settings.BusSpeed = I2cBusSpeed.FastMode;

    // Create an I2cDevice with the specified I2C settings
    var controller = await I2cController.GetDefaultAsync();

    using (I2cDevice device = controller.GetDevice(settings))
    {
        byte[] writeBuf = { 0x01, 0x02, 0x03, 0x04 };
        device.Write(writeBuf);
    }
}

SPI Bus

RPi2/3에는 두 개의 SPI 버스 컨트롤러를 사용할 수 있습니다.

SPI0

신호 이름 머리글 고정 번호 Gpio 번호
Mosi 19 10
미소 21 9
Sclk 23 11
CS0 24 8
CS1 26 7

SPI1

신호 이름 머리글 고정 번호 Gpio 번호
Mosi 38 20
미소 35 19
Sclk 40 21
CS0 36 16

SPI 샘플

칩 선택 0을 사용하여 버스 SPI0 에서 SPI 쓰기를 수행하는 방법의 예는 다음과 같습니다.

using Windows.Devices.Enumeration;
using Windows.Devices.Spi;

public async void SPI()
{
    // Use chip select line CS0
    var settings = new SpiConnectionSettings(0);
    // Set clock to 10MHz
    settings.ClockFrequency = 10000000;

    // Get a selector string that will return our wanted SPI controller
    string aqs = SpiDevice.GetDeviceSelector("SPI0");

    // Find the SPI bus controller devices with our selector string
    var dis = await DeviceInformation.FindAllAsync(aqs);

    // Create an SpiDevice with our selected bus controller and Spi settings
    using (SpiDevice device = await SpiDevice.FromIdAsync(dis[0].Id, settings))
    {
        byte[] writeBuf = { 0x01, 0x02, 0x03, 0x04 };
        device.Write(writeBuf);
    }
}