在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
查看: 4858|回复: 1

[活动] [Zynq征文]helloworld程序参考

[复制链接]
发表于 2013-1-26 12:54:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
#include <stdio.h>
#include "platform.h"
#include "xil_types.h"
#include "xgpio.h"
#include "xtmrctr.h"
#include "xparameters.h"
#include "xgpiops.h"
#include "xil_io.h"
#include "xil_exception.h"
#include "xscugic.h"
static XGpioPs psGpioInstancePtr;
extern XGpioPs_Config XGpioPs_ConfigTable[XPAR_XGPIOPS_NUM_INSTANCES];
static int iPinNumber = 7; /*Led LD9 is connected to MIO pin 7*/
XScuGic InterruptController; /* Instance of the Interrupt Controller */
static XScuGic_Config *GicConfig;/* The configuration parameters of the
            controller */
static int InterruptFlag;
void print(char *str);
extern char inbyte(void);
void Timer_InterruptHandler(void *data, u8 TmrCtrNumber)
{
  print("\r\n");
  print("\r\n");
  print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n");
  print(" Inside Timer ISR \n \r ");
  XTmrCtr_Stop(data,TmrCtrNumber);
  // PS GPIO Writing
  print("LED 'LD9' Turned ON \r\n");
  XGpioPs_WritePin(&psGpioInstancePtr,iPinNumber,1);
  XTmrCtr_Reset(data,TmrCtrNumber);
  print(" Timer ISR Exit\n \n \r");
  print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n");
  print("\r\n");
  print("\r\n");
  InterruptFlag = 1;
}
int SetUpInterruptSystem(XScuGic *XScuGicInstancePtr)
{
  /*
   * Connect the interrupt controller interrupt handler to the hardware
   * interrupt handling logic in the arm processor.
   */
  Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
      (Xil_ExceptionHandler) XScuGic_InterruptHandler,
      XScuGicInstancePtr);
  /*
   * Enable interrupts in the ARM
   */
  Xil_ExceptionEnable();
  return XST_SUCCESS;
}
int ScuGicInterrupt_Init(u16 DeviceId,XTmrCtr *TimerInstancePtr)
{
  int Status;
  /*
   * Initialize the interrupt controller driver so that it is ready to
   * use.
   */
  GicConfig = XScuGic_LookupConfig(DeviceId);
  if (NULL == GicConfig) {
    return XST_FAILURE;
  }
  Status = XScuGic_CfgInitialize(&InterruptController, GicConfig,
      GicConfig->cpuBaseAddress);
  if (Status != XST_SUCCESS) {
    return XST_FAILURE;
  }
  /*
   * Setup the Interrupt System
   */
  Status = SetUpInterruptSystem(&InterruptController);
  if (Status != XST_SUCCESS) {
    return XST_FAILURE;
  }
  /*
   * Connect a device driver handler that will be called when an
   * interrupt for the device occurs, the device driver handler performs
   * the specific interrupt processing for the device
   */
  Status = XScuGic_Connect(&InterruptController,
      XPAR_FABRIC_AXI_TIMER_0_INTERRUPT_INTR,
      (Xil_ExceptionHandler)XTmrCtr_InterruptHandler,
      (void *)TimerInstancePtr);
  if (Status != XST_SUCCESS) {
    return XST_FAILURE;
  }
  /*
   * Enable the interrupt for the device and then cause (simulate) an
   * interrupt so the handlers will be called
   */
  XScuGic_Enable(&InterruptController, XPAR_FABRIC_AXI_TIMER_0_INTERRUPT_INTR);
  return XST_SUCCESS;
}
int main()
{
  static XGpio GPIOInstance_Ptr;
  XGpioPs_Config*GpioConfigPtr;
  XTmrCtr TimerInstancePtr;
  int xStatus;
  u32 Readstatus=0,OldReadStatus=0;
  //u32 EffectiveAdress = 0xE000A000;
  int iPinNumberEMIO = 54;
  u32 uPinDirectionEMIO = 0x0;
  // Input Pin
  // Pin direction
  u32 uPinDirection = 0x1;
  int exit_flag,choice,internal_choice;
  init_platform();
  /* data = *(u32 *)(0x42800004);
     print("OK \n");
     data = *(u32 *)(0x41200004);
     print("OK-1 \n");
   */
  print("##### Application Starts #####\n\r");
  print("\r\n");
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-1 :AXI GPIO Initialization
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  xStatus = XGpio_Initialize(&GPIOInstance_Ptr,XPAR_AXI_GPIO_0_DEVICE_ID);
  if(XST_SUCCESS != xStatus)
    print("GPIO INIT FAILED\n\r");
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-2 :AXI GPIO Set the Direction
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  XGpio_SetDataDirection(&GPIOInstance_Ptr, 1,1);
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-3 :AXI Timer Initialization
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  xStatus = XTmrCtr_Initialize(&TimerInstancePtr,XPAR_AXI_TIMER_0_DEVICE_ID);
  if(XST_SUCCESS != xStatus)
    print("TIMER INIT FAILED \n\r");
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-4 :Set Timer Handler
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  XTmrCtr_SetHandler(&TimerInstancePtr,
      Timer_InterruptHandler,
      &TimerInstancePtr);
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-5 :Setting timer Reset Value
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  XTmrCtr_SetResetValue(&TimerInstancePtr,
      0, //Change with generic value
      0xf0000000);
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-6 :Setting timer Option (Interrupt Mode And Auto Reload )
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  XTmrCtr_SetOptions(&TimerInstancePtr,
      XPAR_AXI_TIMER_0_DEVICE_ID,
      (XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION ));
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-7 S GPIO Intialization
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  GpioConfigPtr = XGpioPs_LookupConfig(XPAR_PS7_GPIO_0_DEVICE_ID);
  if(GpioConfigPtr == NULL)
    return XST_FAILURE;
  xStatus = XGpioPs_CfgInitialize(&psGpioInstancePtr,
      GpioConfigPtr,
      GpioConfigPtr->BaseAddr);
  if(XST_SUCCESS != xStatus)
    print(" PS GPIO INIT FAILED \n\r");
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-8 S GPIO pin setting to Output
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  XGpioPs_SetDirectionPin(&psGpioInstancePtr, iPinNumber,uPinDirection);
  XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, iPinNumber,1);
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-9 :EMIO PIN Setting to Input port
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  XGpioPs_SetDirectionPin(&psGpioInstancePtr,
      iPinNumberEMIO,uPinDirectionEMIO);
  XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, iPinNumberEMIO,0);
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-10 : SCUGIC interrupt controller Initialization
  //Registration of the Timer ISR
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  xStatus=
      ScuGicInterrupt_Init(XPAR_PS7_SCUGIC_0_DEVICE_ID,&TimerInstancePtr);
  if(XST_SUCCESS != xStatus)
    print(" SCUGIC INIT FAILED \n\r");
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Step-11 :User selection procedure to select and execute tests
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  exit_flag = 0;
  while(exit_flag != 1)
  {
    print(" SELECT the Operation from the Below Menu \r\n");
    print("###################### Menu Starts ########################\r\n");
    print("ress '1' to use NORMAL GPIO as an input (BTNU switch)\r\n");
    print("ress '2' to use EMIO as an input (BTNR switch)\r\n");
    print("ress any other key to Exit\r\n");
    print(" ##################### Menu Ends #########################\r\n");
    choice = inbyte();
    printf("Selection : %c \r\n",choice);
    internal_choice = 1;
    switch(choice)
    {
    //~~~~~~~~~~~~~~~~~~~~~~~
    // Use case for AXI GPIO
    //~~~~~~~~~~~~~~~~~~~~~~~~
    case '1':
      exit_flag = 0;
      print("Press Switch 'BTNU' push button on board \r\n");
      print(" \r\n");
      while(internal_choice != '0')
      {
        Readstatus = XGpio_DiscreteRead(&GPIOInstance_Ptr, 1);
        if(1== Readstatus && 0 == OldReadStatus )
        {
          print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\r\n");
          print("BTNU PUSH Button pressed \n\r");
          print("LED 'LD9' Turned OFF \r\n");
          XGpioPs_WritePin(&psGpioInstancePtr,iPinNumber,0);
          //Start Timer
          XTmrCtr_Start(&TimerInstancePtr,0);
          print("timer start \n\r");
          //Wait For interrupt;
          print("Wait for the Timer interrupt to tigger \r\n");
          print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\r\n");
          print(" \r\n");
          while(InterruptFlag != 1);
          InterruptFlag = 0;
          print(" ###########################################\r\n ");
          print("Press '0' to go to Main Menu \n\r ");
          print("Press any other key to remain in AXI GPIO Test \n\r ");
          print(" ###########################################\r\n ");
          internal_choice = inbyte();
          printf("Select = %c \r\n",internal_choice);
          if(internal_choice != '0')
          {
            print("Press Switch 'BTNU' push button on board \r\n");
          }
        }
        OldReadStatus = Readstatus;
      }
      print(" \r\n");
      print(" \r\n");
      break;
    case '2' :
      //~~~~~~~~~~~~~~~~~~~~~~~
      //Usecase for PS GPIO
      //~~~~~~~~~~~~~~~~~~~~~~~~
      exit_flag = 0;
      print("Press Switch 'BTNR' push button on board \r\n");
      print(" \r\n");
      while(internal_choice != '0')
      {
        Readstatus = XGpioPs_ReadPin(&psGpioInstancePtr,
            iPinNumberEMIO);
        if(1== Readstatus && 0 == OldReadStatus )
        {
          print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\r\n");
          print("BTNR PUSH Button pressed \n\r");
          print("LED 'LD9' Turned OFF \r\n");
          XGpioPs_WritePin(&psGpioInstancePtr,iPinNumber,0);
          //Start Timer
          XTmrCtr_Start(&TimerInstancePtr,0);
          print("timer start \n\r");
          //Wait For interrupt;
          print("Wait for the Timer interrupt to tigger \r\n");
          print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\r\n");
          print(" \r\n");
          while(InterruptFlag != 1);
          InterruptFlag = 0;
          print(" ###########################################\r\n ");
          print("Press '0' to go to Main Menu \n\r ");
          print("Press any other key to remain in EMIO Test \n\r ");
          print(" ###########################################\r\n ");
          internal_choice = inbyte();
          printf("Select = %c \r\n",internal_choice);
          if(internal_choice != '0')
          {
            print("Press Switch 'BTNR' push button on board \r\n");
          }
        }
        OldReadStatus = Readstatus;
      }
      print(" \r\n");
      print(" \r\n");
      break;
    default :
      exit_flag = 1;
      break;
    }
  }
  print("\r\n");
  print("***********\r\n");
  print("BYE \r\n");
  print("***********\r\n");
  cleanup_platform();
  return 0;
}
发表于 2014-2-7 00:25:52 | 显示全部楼层
用来给内部
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

×

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-3-29 02:18 , Processed in 0.031888 second(s), 8 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表