PYNQ-關於PYNQ的GPIO的使用(RPI接口和arduino接口)或者常用的IO設備(如UART SPI IIC TIMER)

學習內容

  1. PYNQ的串口使用拓展
  2. GPIO的配置
  3. 類比配置別的IO功能

開發環境

PYNQ 這裏我用的是2.3的官方鏡像,jupyter-Notebook

官方文檔參考

[https://pynq.readthedocs.io/en/latest/pynq_libraries.html#pynqmicroblaze]

1.PYNQ的串口使用拓展

前文提到了uart的配置的方式是用的RPI接口,下面我將簡單說一下用arduino接口。別的地方的配置和前文的串口的配置相同吧,這裏只需要把MBlibrary加載的ip改爲arduino,如文:

lib = MicroblazeLibrary(base.ARDUINO, ['uart'])
device = lib.uart_open(0,1)

這裏要說明下uart管腳的定義配置,這裏是說如何進行編號:給出原理圖
在這裏插入圖片描述
從右到左 從下到上由0 遞增編號

AxiGPIO

AxiGPIO類提供了從外部通用外圍設備(如LED,按鈕,使用AXI GPIO控制器IP連接到PL的開關)讀取,寫入和接收中斷的方法。
在這裏插入圖片描述
AxiGPIO模塊控制PL中AXI GPIO控制器的實例。每個AXI GPIO最多可具有兩個通道,每個通道最多具有32個引腳。
在這裏插入圖片描述
read()和write()方法用於一個信道(所有的GPIO的)上讀取和寫入數據。
setdirection()並setlength()可以用來配置IP。方向可以是“內”,“外”和“內”。
默認情況下,方向是“ inout”。指定“入”或“出”將僅分別允許對IP進行讀取和寫入,而嘗試讀取 “出”或寫入 “入”將導致錯誤。
可以將長度設置爲僅寫入較小範圍的GPIO。
GPIO也可以視爲數組。這允許設置特定的位,並且避免使用位掩碼。
來自AXI GPIO 的中斷信號ip2intc_irpt可以直接連接到AXI中斷控制器,以在PS中引起中斷。有關AsyncIO和中斷的更多信息,請參見PYNQ和Asyncio 部分。

LED,Switch,Button和RGBLED類擴展了AxiGPIO控制器,並針對相應的外設進行了自定義。這些類期望[led|switch|button|rgbleds]_gpio 在與此類一起使用的覆蓋中存在被稱爲的AXI GPIO實例。

例子
該示例僅用於說明,以顯示如何使用AxiGPIO類。實際上,LED,按鈕,開關和RGBLED類可用於擴展AxiGPIO類,這些類應用於覆蓋層中的這些外設。

加載覆蓋後,可以通過將AXI GPIO控制器的名稱傳遞給該類來實例化AxiGPIO實例。

加載overlay:

from pynq import Overlay
from pynq.lib import AxiGPIO
ol = Overlay("base.bit")

配置io

led_ip = ol.ip_dict['gpio_leds']
switches_ip = ol.ip_dict['gpio_switches']
leds = AxiGPIO(led_ip).channel1
switches = AxiGPIO(switches_ip).channel1

簡單的讀寫:

mask = 0xffffffff
leds.write(0xf, mask)
switches.read()

使用AXI GPIO作爲數組:

switches.setdirection("in")
switches.setlength(3)
switches.read()

這種是官方給的AXI GPIO的例子,這裏我沒有經過實踐驗證,所以,我這裏就不多說明。下面將介紹另外一種方法,和前文應用方式相同

MB調用GPIO

在這裏插入圖片描述
通樣是打開官方的文檔查看在MB裏有哪些可以進行配置的操作,這裏我們可以進行調用
GPIO Devices
GPIO devices allow for one or multiple pins to be read and written directly. All of these functions are in gpio.h

gpio type
A handle to one or more pins which can be set simultaneously.

gpio gpio_open(int pin)
Returns a new handle to a GPIO device for a specific pin on the I/O switch. This function can only be called if there is an I/O switch in the design.

gpio gpio_open_device(unsigned int device)
Returns a handle to an AXI GPIO controller based either on the base address or device index. The handle will allow for all pins on channel 1 to be set simultaneously.

gpio gpio_configure(gpio parent, int low, int hi, int channel)
Returns a new handle tied to the specified pins of the controller. This function does not change the configuration of the parent handle.

void gpio_set_direction(gpio device, int direction)
Sets the direction of all pins tied to the specified handle. The direction can either be GPIO_IN or GPIO_OUT.

void gpio_write(gpio device, unsigned int value)
Sets the value of the output pins represented by the handle. If the handle represents multiple pins then the least significant bit refers to the lowest index pin. Writing to pins configured as input has no effect.

unsigned int gpio_read(gpio device)
Reads the value of input pins represented by the handle, If the handle represents multiple pins then the least significant bit refers to the lowest index pin. Read from pins configured as output results in 0 being returned.

void gpio_close(gpio_device)
Returns the specified pins to high-impedance output and closes the device.

函數使用

這裏我們需要用到的是gpio gpio_open(int pin)void gpio_set_direction(gpio device, int direction)void gpio_write(gpio device, unsigned int value)unsigned int gpio_read(gpio device)
這些函數比較好理解我就不過多說明了,直接看下配置程序:

首先加載官方的overlays,並且把mb.lib包含下:

from pynq.overlays.base import BaseOverlay
from pynq.lib import MicroblazeLibrary
base = BaseOverlay('base.bit')
print('finish')

打開GPIO

#如果用別的接口只需要把這裏的RPI改爲相應名稱。

lib = MicroblazeLibrary(base.RPI, ['gpio'])
device = lib.gpio_open(14)
device1 = lib.gpio_open(15)

輸入輸出配置

gpio_set_direction(device, 1)#輸入
gpio_set_direction(device1, 0)#輸出

GPIO寫操作

#只可以進行寫0,寫1
gpio_write(device, 1)

GPIO讀操作

#可以用py打印下讀到的值
read_num=gpio_write(device1)
print(read_num)

IO操作的拓展

其實可以從前文的uart和本文的gpio操作進行類比在官方文檔中的配置,文檔中都有詳細的說明,只要慢慢啃就可以,這裏像別的SPI IIC的io配置都可以類似這更改這裏:

lib = MicroblazeLibrary(base.接口, ['設備'])

即可
比如這裏如果想利用RPI的iic接口:

lib = MicroblazeLibrary(base.RPI, ['iic'])

如果想並用gpio和uart那麼只需要在list增加即可,如:

lib = MicroblazeLibrary(base.RPI, ['gpio','uart'])

別的操作大家可以自己嘗試,好了就爲大家介紹到這裏。

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