本章介绍PS端的MIO操作,MIO是基础的外设IO,可以连接诸如SPI,I2C,UART,GPIO等,通过Vivado软件设置,软件可以将PS端信号通过MIO导出,同样可以将信号通过EMIO连接到PL端的引脚上。
因为需要控制PS端的MIO,所以需要将GPIO MIO打开,需要配置。
MIO共有两个BANK,BANK0有有16个引脚,BANK1为38个引脚,共计54个引脚
7020开发板BANK介绍,分别连接PS端54脚和PL端64脚
BANK0控制32个信号,BANK1控制22个信号总共MIO有54个引脚也就是常用的SPI,I2C,USB,SD等。
BANK2和BANK3共能控制64个PL端引脚,注意每一组都有三个信号,输入EMIOGPIO1,输出EMIOGPIO0,输出使能EMIOGPIOTH,类似三态门,共计192个信号,可以连接到PL端引脚,通过PS端控制。
点击Launch SDK,打开SDK进行开发
一、查看原理图可以得知7020的LED连接到了PS端的MIO0和MIO13上,可以根据相应的开发板MIO的位置控制LED灯。
二、新建工程默认hello world工程,工程名字为ps_led_test
下图为GPIO的控制框图,实验中会用到输出部分的寄存器,数据寄存器DATA,数据掩码寄存器MASK_DATA_LSW,MASK_DATA_MSW,方向控制寄存器DIRM,输出使能控制器OEN。
数据掩码寄存器MASK_DATA_0_LSW、MASK_DATA_0_MSW,方向寄存器DIRM、输出使能寄存器OEN_0,数据寄存器DATA_0。
三、查看赛灵思提供的example工程,点开system.mss,打开ps7_gpio中的Import Examples,
在弹出的窗口中选择“xgpios_polled_example”点击OK
这个example工程师测试PS端MIO的输入输出,由于开发板PS端的LED是MIO0和MIO13,需要在文件中修改Output_pin为0,测试MIO的LED灯。
四、选中example的工程,右键进入Run Configurations。
双击System Debugger
10.选中Reset entire system,复位整个系统,即可看到PS_LED1闪烁16次。
新建LED灯控制例子
在ps_led_test的helloworld.c中修改,开发步骤为初始化GPIO、设置方向、输出使能、控制GPIO输出值。
/*
* helloworld.c: simple test application
*
* This application configures UART 16550 to baud rate 9600.
* PS7 UART (Zynq) is not initialized by this application, since
* bootrom/bsp configures it to baud rate 115200
*
* ------------------------------------------------
* | UART TYPE BAUD RATE |
* ------------------------------------------------
* uartns550 9600
* uartlite Configurable only in HW design
* ps7_uart 115200 (configured by bootrom/bsp)
*/
#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xgpiops.h"
#include "sleep.h"
#define GPIO_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID
/*
* The following are declared globally so they are zeroed and can be
* easily accessible from a debugger.
*/
XGpioPs Gpio; /* The driver instance for GPIO Device. */
int main()
{
init_platform();
int Status;
XGpioPs_Config *ConfigPtr;
print("Hello World\n\r");
/* Initialize the GPIO driver. */
ConfigPtr = XGpioPs_LookupConfig(GPIO_DEVICE_ID);
Status = XGpioPs_CfgInitialize(&Gpio, ConfigPtr,
ConfigPtr->BaseAddr);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Set the direction for the pin to be output and
* Enable the Output enable for the LED Pin.
*/
XGpioPs_SetDirectionPin(&Gpio, 0, 1);
XGpioPs_SetDirectionPin(&Gpio, 13, 1);
XGpioPs_SetOutputEnablePin(&Gpio, 0, 1);
XGpioPs_SetOutputEnablePin(&Gpio, 13, 1);
while(1){
/* Set the GPIO output to be low. */
XGpioPs_WritePin(&Gpio, 0, 0x0);
XGpioPs_WritePin(&Gpio, 13, 0x1);
sleep(1) ;
/* Set the GPIO output to be high. */
XGpioPs_WritePin(&Gpio, 0, 0x1);
XGpioPs_WritePin(&Gpio, 13, 0x0);
sleep(1) ;
}
cleanup_platform();
return 0;
}
点击运行
五.按键中断
前面介绍了MIO作为输出控制LED灯,这里利用MIO作为按键控制LED灯。
中断寄存器:
INT_MASK:中断掩码
INT_DIS:中断关闭
INT_EN:中断使能
INT_TYPE:中断类型,设置电平敏感还是边沿敏感
INT_POLARITY:中断极性,设置低电平或下降沿还是高电平或上升沿
INT_ANY:边沿触发方式,需要INT_TYPE设置为边沿敏感才能使用
设置中断产生方式需要INT_TYPE、INT_POLARITY、INT_ANY配合使用。
在原理图中可以看到PS端的按键接在了MIO50和MIO51上,本实验采用MIO50。
六、本实验设计为按下按键LED灯亮 再按下LED灯灭
主程序设计流程:
GPIO初始化->设置按键和LED方向->设置产生中断方式->设置中断->打开中断控制器->打开中断异常->打开GPIO中断->判断KEY_FLAG值,是1,写LED->查询中断状态寄存器->判断状态->清除中断->设置KEY_FLAG值。
七、新建SDK工程
定义PS按键编号为50,PS LED为0
在main函数中,设置LED和按键,将按键中断类型设置为上升沿产生中断。在本实验中,即按键信号的上升沿产生中断。
代码:
/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/
/*
* helloworld.c: simple test application
*
* This application configures UART 16550 to baud rate 9600.
* PS7 UART (Zynq) is not initialized by this application, since
* bootrom/bsp configures it to baud rate 115200
*
* ------------------------------------------------
* | UART TYPE BAUD RATE |
* ------------------------------------------------
* uartns550 9600
* uartlite Configurable only in HW design
* ps7_uart 115200 (configured by bootrom/bsp)
*/
#include "xparameters.h"
#include "xscugic.h"
#include "xgpiops.h"
#include "xil_printf.h"
#include "xil_exception.h"
/* GPIO paramter */
#define MIO_ID XPAR_PS7_GPIO_0_DEVICE_ID
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID
#define KEY_INTR_ID XPAR_XGPIOPS_0_INTR
#define PS_KEY_MIO 50
#define PS_LED_MIO 0
#define GPIO_INPUT 0
#define GPIO_OUTPUT 1
int key_flag ;
XGpioPs GPIO_PTR ;
XScuGic INTCInst;
int IntrInitFuntion(XScuGic *InstancePtr, u16 DeviceId, XGpioPs *GpioInstancePtr);
void GpioHandler(void *CallbackRef);
int main()
{
XGpioPs_Config *GpioConfig ;
int Status ;
int key_val = 0 ;
key_flag = 0 ;
/*
* Initialize the gpio.
*/
GpioConfig = XGpioPs_LookupConfig(MIO_ID) ;
Status = XGpioPs_CfgInitialize(&GPIO_PTR, GpioConfig, GpioConfig->BaseAddr) ;
if (Status != XST_SUCCESS)
{
return XST_FAILURE ;
}
/*
* Set the direction for the pin to be output and
* Enable the Output enable for the LED Pin.
*/
XGpioPs_SetDirectionPin(&GPIO_PTR, PS_LED_MIO, GPIO_OUTPUT) ;
XGpioPs_SetOutputEnablePin(&GPIO_PTR, PS_LED_MIO, GPIO_OUTPUT) ;
/*
* Set the direction for the pin to be input.
* Set interrupt type as rising edge and enable gpio interrupt
*/
XGpioPs_SetDirectionPin(&GPIO_PTR, PS_KEY_MIO, GPIO_INPUT) ;
XGpioPs_SetIntrTypePin(&GPIO_PTR, PS_KEY_MIO, XGPIOPS_IRQ_TYPE_EDGE_RISING) ;
XGpioPs_IntrEnablePin(&GPIO_PTR, PS_KEY_MIO) ;
/*
* sets up the interrupt system
*/
Status = IntrInitFuntion(&INTCInst, MIO_ID, &GPIO_PTR) ;
if (Status != XST_SUCCESS)
return XST_FAILURE ;
while(1)
{
if (key_flag)
{
XGpioPs_WritePin(&GPIO_PTR, PS_LED_MIO, key_val) ;
key_val = ~key_val ;
key_flag = 0 ;
}
}
return 0 ;
}
int IntrInitFuntion(XScuGic *InstancePtr, u16 DeviceId, XGpioPs *GpioInstancePtr)
{
XScuGic_Config *IntcConfig;
int Status ;
/*
* Initialize the interrupt controller driver so that it is ready to
* use.
*/
IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
Status = XScuGic_CfgInitialize(InstancePtr, IntcConfig, IntcConfig->CpuBaseAddress) ;
if (Status != XST_SUCCESS)
return XST_FAILURE ;
/*
* set priority and trigger type
*/
XScuGic_SetPriorityTriggerType(InstancePtr, KEY_INTR_ID,
0xA0, 0x3);
/*
* Connect the device driver handler that will be called when an
* interrupt for the device occurs, the handler defined above performs
* the specific interrupt processing for the device.
*/
Status = XScuGic_Connect(InstancePtr, KEY_INTR_ID,
(Xil_ExceptionHandler)GpioHandler,
(void *)GpioInstancePtr) ;
if (Status != XST_SUCCESS)
return XST_FAILURE ;
/*
* Enable the interrupt for the device.
*/
XScuGic_Enable(InstancePtr, KEY_INTR_ID) ;
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler,
InstancePtr);
Xil_ExceptionEnable();
return XST_SUCCESS ;
}
void GpioHandler(void *CallbackRef)
{
XGpioPs *GpioInstancePtr = (XGpioPs *)CallbackRef ;
int Int_val ;
Int_val = XGpioPs_IntrGetStatusPin(GpioInstancePtr, PS_KEY_MIO) ;
/*
* Clear interrupt.
*/
XGpioPs_IntrClearPin(GpioInstancePtr, PS_KEY_MIO) ;
if (Int_val)
key_flag = 1 ;
}
烧写程序,观察现象