📄 xgpiops.c
字号:
/* $Id: xgpiops.c,v 1.1.2.1 2011/01/20 03:42:36 sadanan Exp $ *//******************************************************************************** (c) Copyright 2010-12 Xilinx, Inc. All rights reserved.** This file contains confidential and proprietary information of Xilinx, Inc.* and is protected under U.S. and international copyright and other* intellectual property laws.** DISCLAIMER* This disclaimer is not a license and does not grant any rights to the* materials distributed herewith. Except as otherwise provided in a valid* license issued to you by Xilinx, and to the maximum extent permitted by* applicable law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND WITH ALL* FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS,* IMPLIED, OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF* MERCHANTABILITY, NON-INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE;* and (2) Xilinx shall not be liable (whether in contract or tort, including* negligence, or under any other theory of liability) for any loss or damage* of any kind or nature related to, arising under or in connection with these* materials, including for any direct, or any indirect, special, incidental,* or consequential loss or damage (including loss of data, profits, goodwill,* or any type of loss or damage suffered as a result of any action brought by* a third party) even if such damage or loss was reasonably foreseeable or* Xilinx had been advised of the possibility of the same.** CRITICAL APPLICATIONS* Xilinx products are not designed or intended to be fail-safe, or for use in* any application requiring fail-safe performance, such as life-support or* safety devices or systems, Class III medical devices, nuclear facilities,* applications related to the deployment of airbags, or any other applications* that could lead to death, personal injury, or severe property or* environmental damage (individually and collectively, "Critical* Applications"). Customer assumes the sole risk and liability of any use of* Xilinx products in Critical Applications, subject only to applicable laws* and regulations governing limitations on product liability.** THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS PART OF THIS FILE* AT ALL TIMES.*******************************************************************************//*****************************************************************************//**** @file xgpiops.c** The XGpioPs driver. Functions in this file are the minimum required functions* for this driver. See xgpiops.h for a detailed description of the driver.** <pre>* MODIFICATION HISTORY:** Ver Who Date Changes* ----- ---- -------- -----------------------------------------------* 1.00a sv 01/15/10 First Release* 1.01a sv 04/15/12 Removed the APIs XGpioPs_SetMode, XGpioPs_SetModePin* XGpioPs_GetMode, XGpioPs_GetModePin as they are not* relevant to Zynq device. The interrupts are disabled* for output pins on all banks during initialization.* </pre>*******************************************************************************//***************************** Include Files *********************************/#include "xgpiops.h"/************************** Constant Definitions *****************************//**************************** Type Definitions *******************************//***************** Macros (Inline Functions) Definitions *********************//************************** Variable Definitions *****************************//* * This structure defines the mapping of the pin numbers to the banks when * the driver APIs are used for working on the individual pins. */unsigned int XGpioPsPinTable[] = { 31, /* 0 - 31, Bank 0 */ 53, /* 32 - 53, Bank 1 */ 85, /* 54 - 85, Bank 2 */ 117 /* 86 - 117 Bank 3 */};/************************** Function Prototypes ******************************/extern void StubHandler(void *CallBackRef, int Bank, u32 Status);/*****************************************************************************//*** 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 XGpioPs_CfgInitialize(XGpioPs *InstancePtr, XGpioPs_Config *ConfigPtr, u32 EffectiveAddr){ Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(ConfigPtr != NULL); /* * Set some default values for instance data, don't indicate the device * is ready to use until everything has been initialized successfully. */ InstancePtr->IsReady = 0; InstancePtr->GpioConfig.BaseAddr = EffectiveAddr; InstancePtr->GpioConfig.DeviceId = ConfigPtr->DeviceId; InstancePtr->Handler = StubHandler; /* * By default, interrupts are not masked in GPIO. Disable * interrupts for all pins in all the 4 banks. */ XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr, XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFF); XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr, ((1) * XGPIOPS_REG_MASK_OFFSET) + XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFF); XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr, ((2) * XGPIOPS_REG_MASK_OFFSET) + XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFF); XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr, ((3) * XGPIOPS_REG_MASK_OFFSET) + XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFF); /* * Indicate the component is now ready to use. */ InstancePtr->IsReady = XIL_COMPONENT_IS_READY; return XST_SUCCESS;}/****************************************************************************//**** Read the Data register of the specified GPIO bank.** @param InstancePtr is a pointer to the XGpioPs instance.* @param Bank is the bank number of the GPIO to operate on.* Valid values are 0 to XGPIOPS_MAX_BANKS - 1.** @return Current value of the Data register.** @note This function is used for reading the state of all the GPIO pins* of specified bank.******************************************************************************/u32 XGpioPs_Read(XGpioPs *InstancePtr, u8 Bank){ Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); Xil_AssertNonvoid(Bank < XGPIOPS_MAX_BANKS); return XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr, ((Bank) * XGPIOPS_DATA_BANK_OFFSET) + XGPIOPS_DATA_OFFSET);}/****************************************************************************//**** Read Data from the specified pin.** @param InstancePtr is a pointer to the XGpioPs instance.* @param Pin is the pin number for which the data has to be read.* Valid values are 0 to XGPIOPS_DEVICE_MAX_PIN_NUM - 1.* See xgpiops.h for the mapping of the pin numbers in the banks.** @return Current value of the Pin (0 or 1).** @note This function is used for reading the state of the specified* GPIO pin.******************************************************************************/int XGpioPs_ReadPin(XGpioPs *InstancePtr, int Pin){ u8 Bank; u8 PinNumber; Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); Xil_AssertNonvoid(Pin < XGPIOPS_DEVICE_MAX_PIN_NUM); /* * Get the Bank number and Pin number within the bank. */ XGpioPs_GetBankPin(Pin, &Bank, &PinNumber); return (XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr, ((Bank) * XGPIOPS_DATA_BANK_OFFSET) + XGPIOPS_DATA_OFFSET) >> PinNumber) & 1;}/****************************************************************************//**** Write to the Data register of the specified GPIO bank.** @param InstancePtr is a pointer to the XGpioPs instance.* @param Bank is the bank number of the GPIO to operate on.* Valid values are 0 to XGPIOPS_MAX_BANKS - 1.* @param Data is the value to be written to the Data register.** @return None.** @note This function is used for writing to all the GPIO pins of* the bank. The previous state of the pins is not maintained.******************************************************************************/void XGpioPs_Write(XGpioPs *InstancePtr, u8 Bank, u32 Data){ Xil_AssertVoid(InstancePtr != NULL); Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); Xil_AssertVoid(Bank < XGPIOPS_MAX_BANKS); XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr, ((Bank) * XGPIOPS_DATA_BANK_OFFSET) + XGPIOPS_DATA_OFFSET, Data);}/****************************************************************************//**** Write data to the specified pin.** @param InstancePtr is a pointer to the XGpioPs instance.* @param Pin is the pin number to which the Data is to be written.* Valid values are 0 to XGPIOPS_DEVICE_MAX_PIN_NUM - 1.* @param Data is the data to be written to the specified pin (0 or 1).** @return None.** @note This function does a masked write to the specified pin of* the specified GPIO bank. The previous state of other pins* is maintained.******************************************************************************/void XGpioPs_WritePin(XGpioPs *InstancePtr, int Pin, int Data){ u32 RegOffset; u32 Value; u8 Bank; u8 PinNumber; Xil_AssertVoid(InstancePtr != NULL); Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); Xil_AssertVoid(Pin < XGPIOPS_DEVICE_MAX_PIN_NUM); /* * Get the Bank number and Pin number within the bank. */ XGpioPs_GetBankPin(Pin, &Bank, &PinNumber); if (PinNumber > 15) { /* * There are only 16 data bits in bit maskable register. */ PinNumber -= 16; RegOffset = XGPIOPS_DATA_MSW_OFFSET; } else { RegOffset = XGPIOPS_DATA_LSW_OFFSET; } /* * Get the 32 bit value to be written to the Mask/Data register where * the upper 16 bits is the mask and lower 16 bits is the data. */ Data &= 0x01; Value = ~(1 << (PinNumber + 16)) & ((Data << PinNumber) | 0xFFFF0000); XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr, ((Bank) * XGPIOPS_DATA_MASK_OFFSET) + RegOffset, Value);}/****************************************************************************//**** Set the Direction of the pins of the specified GPIO Bank.*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -