FPGA学习笔记(40)——Xilinx Vitis API常用部分解析

注:在源代码中 “ctrl+单击” 可查看详细信息

一.头文件


​ #include “xparameters.h” //器件参数信息 ​ #include “xstatus.h” //包含 XST_FAILURE 和 XST_SUCCESS 的宏定义 ​ #include “xil_printf.h” //包含 print()函数 ​ #include “xgpiops.h” //包含 PS GPIO 的函数 ​ #include “sleep.h” //包含 sleep()函数

二.宏定义


​ //PS_LED1 连接到 MIO38 ​ #define MIO_LED1 38 ​
//宏定义 GPIO_DEVICE_ID #define GPIO_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID

三.函数

1.配置

<1>器件初始化


​ //PS 端 GPIO 驱动实例 ​ XGpioPs gpiops_inst; ​ //PS 端 GPIO 配置信息 ​ XGpioPs_Config *gpiops_cfg_ptr;


​ //根据器件 ID 查找配置信息 ​ gpiops_cfg_ptr = XGpioPs_LookupConfig(GPIOPS_ID); ​ //初始化器件驱动 ​ Status = XGpioPs_CfgInitialize(&Gpio, ConfigPtr,ConfigPtr->BaseAddr); ​ //判断是否初始化成功 ​ if (Status != XST_SUCCESS)return XST_FAILURE;






​ XGpioPs_Config XGpioPs_LookupConfig(u16 DeviceId); ​ /* ​ * ​ * This function looks for the device configuration based on the unique device ​ * ID. The table XGpioPs_ConfigTable[] contains the configuration information ​ * for each device in the system. ​ * ​ * @param DeviceId is the unique device ID of the device being looked up. ​ * ​ * @return A pointer to the configuration table entry corresponding to the ​ * given device ID, or NULL if no match is found. ​ * ​ * @note None. ​ * ​ ******************************************************************************/



​ s32 XGpioPs_CfgInitialize(XGpioPs InstancePtr, const XGpioPs_Config ConfigPtr,u32 EffectiveAddr); ​ /*****************************************************************************/ ​ / ​ * ​ * This function initializes a XGpioPs instance/driver. ​ * All members of the XGpioPs instance structure are initialized and ​ * StubHandlers are assigned to the Bank Status Handlers. ​ * ​ * @param InstancePtr is a pointer to the XGpioPs instance. ​ * @param ConfigPtr points to the XGpioPs device configuration structure. ​ * @param EffectiveAddr is the device base address in the virtual memory ​ * address space. If the address translation is not used then the ​ * physical address should be passed. ​ * Unexpected errors may occur if the address mapping is changed ​ * after this function is invoked. ​ * ​ * @return XST_SUCCESS always. ​ * ​ * @note None. ​ * ​ ******************************************************************************/



​ int XGpio_Initialize(XGpio InstancePtr, u16 DeviceId); ​ /***************************************************************************/ ​ /** ​ * Initialize the XGpio instance provided by the caller based on the ​ * given DeviceID. ​ * ​ * Nothing is done except to initialize the InstancePtr. ​ * ​ * @param InstancePtr is a pointer to an XGpio instance. The memory the ​ * pointer references must be pre-allocated by the caller. Further ​ * calls to manipulate the instance/driver through the XGpio API ​ * must be made with this pointer. ​ * @param DeviceId is the unique id of the device controlled by this XGpio ​ * instance. Passing in a device id associates the generic XGpio ​ * instance to a specific device, as chosen by the caller or ​ * application developer. ​ * ​ * @return ​ * - XST_SUCCESS if the initialization was successful. ​ * - XST_DEVICE_NOT_FOUND if the device configuration data was not ​ * found for a device with the supplied device ID. ​ * ​ * @note None. ​ * ​ *****************************************************************************/

<2>中断配置


​ //建立中断系统,使能KEY按键的下降沿中断 ​ // @param GicInstancePtr是一个指向XScuGic驱动实例的指针 ​ // @param gpio是一个指向连接到中断的GPIO组件实例的指针 ​ // @param GpioIntrId是Gpio中断ID ​ // @return 如果成功返回XST_SUCCESS, 否则返回XST_FAILURE ​ int setup_interrupt_system(XScuGic *gic_ins_ptr, XGpioPs *gpio, u16 GpioIntrId) ​ { ​ int status; ​ XScuGic_Config *IntcConfig; //中断控制器配置信息 ​
//查找中断控制器配置信息并初始化中断控制器驱动 IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID); if (NULL == IntcConfig) { return XST_FAILURE; }

    status = XScuGic_CfgInitialize(gic_ins_ptr, IntcConfig,
            IntcConfig->CpuBaseAddress);
    if (status != XST_SUCCESS) {
        return XST_FAILURE;
    }


​ //设置并使能中断异常 ​ Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, ​ (Xil_ExceptionHandler) XScuGic_InterruptHandler, gic_ins_ptr); ​ Xil_ExceptionEnable(); ​ //为中断设置中断处理函数 ​ status = XScuGic_Connect(gic_ins_ptr, GpioIntrId, ​ (Xil_ExceptionHandler) intr_handler, (void *) gpio); ​ if (status != XST_SUCCESS) { ​ return status; ​ } ​ //使能来自于Gpio器件的中断 ​ XScuGic_Enable(gic_ins_ptr, GpioIntrId); ​ //设置KEY按键的中断类型为下降沿中断,按ctrl+单击查看其他类型 ​ XGpioPs_SetIntrTypePin(gpio, KEY, XGPIOPS_IRQ_TYPE_EDGE_FALLING); ​ //使能按键KEY中断 ​ XGpioPs_IntrEnablePin(gpio, KEY); ​
return XST_SUCCESS; }


​ //设置中断优先级和触发类型(高电平触发) ​ XScuGic_SetPriorityTriggerType(&scugic_inst, GPIO_INT_ID, 0xA0, 0x1);

中断处理函数:


​ //中断处理函数 ​ // @param CallBackRef是指向上层回调引用的指针 ​ static void intr_handler(void *callback_ref) ​ { ​ XGpioPs *gpio = (XGpioPs *) callback_ref; ​
//读取KEY按键引脚的中断状态,判断是否发生中断 if (XGpioPs_IntrGetStatusPin(gpio, KEY)){ key_press = TRUE; XGpioPs_IntrDisablePin(gpio, KEY); //屏蔽按键KEY中断 //XGpioPs_IntrClearPin(&gpio, KEY); //清除按键KEY中断 //XGpioPs_IntrEnablePin(&gpio, KEY); //使能按键KEY中断 } }

<3>uart配置


​ //UART初始化函数 ​ int uart_init(XUartPs* uart_ps) ​ { ​ int status; ​ XUartPs_Config *uart_cfg; ​
uart_cfg = XUartPs_LookupConfig(UART_DEVICE_ID); if (NULL == uart_cfg) return XST_FAILURE; status = XUartPs_CfgInitialize(uart_ps, uart_cfg, uart_cfg->BaseAddress); if (status != XST_SUCCESS) return XST_FAILURE;

    //UART设备自检
    status = XUartPs_SelfTest(uart_ps);
    if (status != XST_SUCCESS)
        return XST_FAILURE;

    //设置工作模式:正常模式
    XUartPs_SetOperMode(uart_ps, XUARTPS_OPER_MODE_NORMAL);
    //设置波特率:115200
    XUartPs_SetBaudRate(uart_ps,115200);
    //设置RxFIFO的中断触发等级
    XUartPs_SetFifoThreshold(uart_ps, 1);

    return XST_SUCCESS;
}

中断函数


​ //UART中断处理函数 ​ void uart_intr_handler(void *call_back_ref) ​ { ​ XUartPs *uart_instance_ptr = (XUartPs *) call_back_ref; ​ u32 rec_data = 0 ; ​ u32 isr_status ; //中断状态标志 ​
//读取中断ID寄存器,判断触发的是哪种中断 isr_status = XUartPs_ReadReg(uart_instance_ptr->Config.BaseAddress, XUARTPS_IMR_OFFSET); isr_status &= XUartPs_ReadReg(uart_instance_ptr->Config.BaseAddress, XUARTPS_ISR_OFFSET);

    //判断中断标志位RxFIFO是否触发
    if (isr_status & (u32)XUARTPS_IXR_RXOVR){
    	rec_data = XUartPs_RecvByte(XPAR_PSU_UART_0_BASEADDR);
        //清除中断标志
        XUartPs_WriteReg(uart_instance_ptr->Config.BaseAddress,
                XUARTPS_ISR_OFFSET, XUARTPS_IXR_RXOVR) ;
    }
    XUartPs_SendByte(XPAR_PSU_UART_0_BASEADDR,rec_data);
}

2.GPIO操作


​ //设置指定引脚的方向:0 输入,1 输出 ​ XGpioPs_SetDirectionPin(&Gpio, MIOLED0, 1); ​ //使能指定引脚输出:0 禁止输出使能,1 使能输出 ​ XGpioPs_SetOutputEnablePin(&Gpio, MIOLED0, 1); ​ //向指定引脚写入数据:0 或 1 ​ XGpioPs_WritePin(&Gpio, MIOLED0, 0x0); ​ //从指定引脚读入数据 ​ XGpioPs_ReadPin(&gpiops_inst, MIO_KEY2)


​ //配置PL AXI GPIO ​ XGpio_SetDataDirection(&axi_gpio_inst, KEY_CHANNEL, 1); //设置PL AXI GPIO 通道1为输入 ​ XGpio_InterruptEnable(&axi_gpio_inst, KEY_MASK); //使能通道1中断 ​ XGpio_InterruptGlobalEnable(&axi_gpio_inst); //使能AXI GPIO全局中断

3.其他


​ //uart打印 ​ print(“MIO Test! \n\r”);

//延时 1 秒 sleep(1);

本文转自 https://blog.csdn.net/qq_32971095/article/details/136315297,如有侵权,请联系删除。

> --------------- THE END -------------- <