CM3計算板I/O編程

1、CM3計算板的IO資源

CM3支持的I/O管腳數爲54個,每個管腳包括一個或多個複用功能,分別位於ALT0~ALT5,如下表:

2、設備樹啓用IO外設的方式

通過在/boot/config.txt 文件中描述IO行爲,可以在系統啓動時,初始化IO外設的初始狀態,例如,配置爲輸入輸出、上下拉狀態以及複選功能。

The gpio directive allows GPIO pins to be set to specific modes and values at boot time in a way that would previously have needed a custom dt-blob.bin file. Each line applies the same settings (or at least makes the same changes) to a set of pins, either a single pin (3), a range of pins (3-4), or a comma-separated list of either (3-4,6,8). The pin set is followed by an = and one or more comma-separated attributes from this list:

  • ip - Input
  • op - Output
  • a0-a5 - Alt0-Alt5
  • dh - Driving high (for outputs)
  • dl - Driving low (for outputs)
  • pu - Pull up
  • pd - Pull down
  • pn/np - No pull

例如:

# 選擇 複用功能2 Alt2 GPIO0~GPIO27
gpio=0-27=a2

# 設置GPIO12 輸出拉高
gpio=12=op,dh

# 設置GPIO18 20 爲輸入上拉
gpio=18,20=pu

# 設置GPIO17~GPIO21 爲輸入
gpio=17-21=ip

3、使用wiringPi C庫對IO編程

  • 安裝最新wiringPi,安裝方法見:安裝wiringPi  
  • 使用gpio readall 查看是否讀取到CM3的所有IO。
  • 編程示例:CM3 GPIO27交替拉高拉低。
  • 編譯: gcc -c testGPIO3.c -o run -l wiringPi
  • 執行:./run
#include <wiringPi.h>

#define TEST_PIN    27

int main(void)
{
    
    wiringPiSetup();
    pinMode(TEST_PIN, OUTPUT);
    
    while(1){

        digitalWrite(TEST_PIN, HIGH);
        delay(1000);

        digitalWrite(TEST_PIN, LOW);
        delay(1000);
        
    }

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章