risc-v中文社区

 找回密码
 立即注册
查看: 2370|回复: 0

[原创] microblaze的中断实验

[复制链接]

347

主题

564

帖子

2237

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2237
发表于 2022-4-8 10:37:54 | 显示全部楼层 |阅读模式
BD图见:http://bbs.risc-v.org.cn/thread-251-1-1.html
实验现象就是:在vivado的Hardware manager中将VIO的输出从0设置为1,则就会进入microblaze的VIO对应的中断函数。
实验图示如下:


实验源码如下:
xbram_example.c:
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2010 - 2014 Xilinx, Inc.  All rights reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  20. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Except as contained in this notice, the name of the Xilinx shall not be used
  24. * in advertising or otherwise to promote the sale, use or other dealings in
  25. * this Software without prior written authorization from Xilinx.
  26. *
  27. ******************************************************************************/
  28. /*****************************************************************************/
  29. /**
  30. * @file xbram_example.c
  31. *
  32. * This file contains a self test example using the BRAM driver (XBram).
  33. *
  34. *
  35. * <pre>
  36. * MODIFICATION HISTORY:
  37. *
  38. * Ver   Who  Date         Changes
  39. * ----- ---- -------- -------------------------------------------------------
  40. * 1.00a sa   05/11/10 Initial release.
  41. * 3.01a sa   13/01/12 Changed XBram_SelfTest(InstancePtr) to
  42. *                          XBram_SelfTest(InstancePtr,0) as per
  43. *                         new API (CR 639274)
  44. * 4.1   ms   01/23/17 Modified xil_printf statement in main function to
  45. *                     ensure that "Successfully ran" and "Failed" strings are
  46. *                     available in all examples. This is a fix for CR-965028.
  47. *</pre>
  48. *
  49. ******************************************************************************/

  50. /***************************** Include Files *********************************/

  51. #include "xparameters.h"
  52. #include "xbram.h"
  53. #include <stdio.h>
  54. #include "sys_intc.h"
  55. #include <xintc.h>
  56. #include <sleep.h>
  57. /************************** Constant Definitions *****************************/

  58. /*
  59. * The following constants map to the XPAR parameters created in the
  60. * xparameters.h file. They are defined here such that a user can easily
  61. * change all the needed parameters in one place.
  62. */
  63. #define BRAM_DEVICE_ID                XPAR_BRAM_0_DEVICE_ID
  64. #define LMB_DEVICE_ID                XPAR_LMB_BRAM_IF_CNTLR_0_DEVICE_ID

  65. /************************** Function Prototypes ******************************/

  66. int BramExample(u16 DeviceId);
  67. static void InitializeECC(XBram_Config *ConfigPtr, u32 EffectiveAddr);


  68. /************************** Variable Definitions *****************************/

  69. /*
  70. * The following are declared globally so they are zeroed and so they are
  71. * easily accessible from a debugger
  72. */
  73. XBram Bram;        /* The Instance of the BRAM Driver */
  74. XBram lmbBram;


  75. #define VIO_INTR_ID                XPAR_INTC_0_DEVICE_ID

  76. static XIntc        Intc;

  77. void VioHandler(void *CallbackRef);
  78. void init_intr_sys(void);

  79. /****************************************************************************/
  80. /**
  81. *
  82. * This function is the main function of the BRAM example.
  83. *
  84. * @param        None.
  85. *
  86. * @return
  87. *                - XST_SUCCESS to indicate success.
  88. *                - XST_FAILURE to indicate failure.
  89. *
  90. * @note                None.
  91. *
  92. *****************************************************************************/
  93. #ifndef TESTAPP_GEN
  94. int main(void)
  95. {
  96.         int Status;

  97.         Status = BramExample(BRAM_DEVICE_ID);
  98.         if (Status != XST_SUCCESS ) {
  99.                 xil_printf("Bram Example Failed\r\n");
  100.                 return XST_FAILURE;
  101.         }

  102.         init_intr_sys();
  103.         while(1){}
  104.         xil_printf("Successfully ran Bram Example\r\n");
  105.         return XST_SUCCESS;
  106. }

  107. void init_intr_sys(void)
  108. {
  109.         Init_Intr_System(&Intc);

  110.         XIntc_Connect(&Intc, VIO_INTR_ID,
  111.                               (Xil_ExceptionHandler)VioHandler, 0);

  112.         Intc_Enable(&Intc, VIO_INTR_ID);
  113.         Intc_Start(&Intc);
  114.         Setup_Intr_Exception(&Intc);
  115. }

  116. void VioHandler(void *CallbackRef)
  117. {
  118.         usleep(5000);
  119. }
  120. #endif

  121. /*****************************************************************************/
  122. /**
  123. *
  124. * This is the entry point for the BRAM example.
  125. *
  126. * @param        DeviceId is the XPAR_<BRAM_instance>_DEVICE_ID value from
  127. *                xparameters.h
  128. *
  129. * @return
  130. *                - XST_SUCCESS to indicate success.
  131. *                - XST_FAILURE to indicate failure.
  132. *
  133. * @note                None.
  134. *
  135. ******************************************************************************/
  136. int BramExample(u16 DeviceId)
  137. {
  138.         int Status;
  139.         XBram_Config *ConfigPtr;

  140.         /*
  141.          * Initialize the BRAM driver. If an error occurs then exit
  142.          */

  143.         /*
  144.          * Lookup configuration data in the device configuration table.
  145.          * Use this configuration info down below when initializing this
  146.          * driver.
  147.          */
  148.         ConfigPtr = XBram_LookupConfig(DeviceId);
  149.         if (ConfigPtr == (XBram_Config *) NULL) {
  150.                 return XST_FAILURE;
  151.         }

  152.         Status = XBram_CfgInitialize(&Bram, ConfigPtr,
  153.                                      ConfigPtr->CtrlBaseAddress);
  154.         if (Status != XST_SUCCESS) {
  155.                 return XST_FAILURE;
  156.         }


  157.         InitializeECC(ConfigPtr, ConfigPtr->CtrlBaseAddress);


  158.         /*
  159.          * Execute the BRAM driver selftest.
  160.          */
  161.         Status = XBram_SelfTest(&Bram, 0);
  162.         if (Status != XST_SUCCESS) {
  163.                 return XST_FAILURE;
  164.         }
  165.         /********joe code start****************/
  166.         XBram_Out8(ConfigPtr->MemBaseAddress,(u8)0x36);
  167.         volatile u8 data = XBram_In8(ConfigPtr->MemBaseAddress);
  168.         XBram_Out8(ConfigPtr->MemBaseAddress,(u8)0x37);
  169.         volatile u8 data2 = XBram_In8(ConfigPtr->MemBaseAddress);
  170. //        if(data != data2)
  171. //        {
  172. //                return XST_SUCCESS;
  173. //        }
  174. //        else
  175. //        {
  176. //                return XST_FAILURE;
  177. //        }
  178.         XBram_Config *lmbConfigPtr;
  179.         lmbConfigPtr = XBram_LookupConfig(LMB_DEVICE_ID);
  180.                 if (lmbConfigPtr == (XBram_Config *) NULL) {
  181.                         return XST_FAILURE;
  182.                 }

  183.                 Status = XBram_CfgInitialize(&lmbBram, lmbConfigPtr,
  184.                                 lmbConfigPtr->CtrlBaseAddress);
  185.                 if (Status != XST_SUCCESS) {
  186.                         return XST_FAILURE;
  187.                 }
  188.                 InitializeECC(lmbConfigPtr, lmbConfigPtr->CtrlBaseAddress);
  189. //                XBram_Out8(lmbConfigPtr->MemBaseAddress,(u8)0x36);
  190.                 data2 = XBram_In8(lmbConfigPtr->MemBaseAddress);
  191.                 XBram_Out8(lmbConfigPtr->MemBaseAddress,(u8)0x37);
  192.                 data = XBram_In8(lmbConfigPtr->MemBaseAddress);
  193.                 if(data != data2)
  194.                 {
  195.                         return XST_SUCCESS;
  196.                 }
  197.                 else
  198.                 {
  199.                         return XST_FAILURE;
  200.                 }
  201.         /********joe code end****************/
  202.         return XST_SUCCESS;
  203. }


  204. /****************************************************************************/
  205. /**
  206. *
  207. * This function ensures that ECC in the BRAM is initialized if no hardware
  208. * initialization is available. The ECC bits are initialized by reading and
  209. * writing data in the memory. This code is not optimized to only read data
  210. * in initialized sections of the BRAM.
  211. *
  212. * @param        ConfigPtr is a reference to a structure containing information
  213. *                about a specific BRAM device.
  214. * @param         EffectiveAddr is the device base address in the virtual memory
  215. *                address space.
  216. *
  217. * @return
  218. *                None
  219. *
  220. * @note                None.
  221. *
  222. *****************************************************************************/
  223. void InitializeECC(XBram_Config *ConfigPtr, u32 EffectiveAddr)
  224. {
  225.         u32 Addr;
  226.         volatile u32 Data;

  227.         if (ConfigPtr->EccPresent &&
  228.             ConfigPtr->EccOnOffRegister &&
  229.             ConfigPtr->EccOnOffResetValue == 0 &&
  230.             ConfigPtr->WriteAccess != 0) {
  231.                 for (Addr = ConfigPtr->MemBaseAddress;
  232.                      Addr < ConfigPtr->MemHighAddress; Addr+=4) {
  233.                         Data = XBram_In32(Addr);
  234.                         XBram_Out32(Addr, Data);
  235.                 }
  236.                 XBram_WriteReg(EffectiveAddr, XBRAM_ECC_ON_OFF_OFFSET, 1);
  237.         }
  238. }
复制代码
sys_intc.h:
  1. #ifndef SYS_INTR_H_
  2. #define SYS_INTR_H_

  3. #include "xparameters.h"
  4. #include "xil_exception.h"
  5. #include "xdebug.h"
  6. #include "XIntc.h"

  7. int Init_Intr_System(XIntc * IntcInstancePtr);
  8. void Intc_Enable(XIntc *IntcInstancePtr,u16 Intr_Id);
  9. int Intc_Start(XIntc *IntcInstancePtr);
  10. void Setup_Intr_Exception(XIntc * IntcInstancePtr);

  11. #endif /* SYS_INTR_H_ */
复制代码
sys_intc.c:
  1. /*
  2. * sys_intc.c
  3. *
  4. *  Created on: 2022年4月8日
  5. *      Author: joe
  6. */


  7. #include "sys_intc.h"
  8. #include "xil_types.h"
  9. #define INTC_DEVICE_ID          XPAR_INTC_0_DEVICE_ID

  10. void Setup_Intr_Exception(XIntc * IntcInstancePtr)
  11. {
  12.         /* Enable interrupts from the hardware */
  13.         Xil_ExceptionInit();
  14.         Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
  15.                         (Xil_ExceptionHandler)XIntc_InterruptHandler,
  16.                         (void *)IntcInstancePtr);

  17.         Xil_ExceptionEnable();
  18. }

  19. int Init_Intr_System(XIntc * IntcInstancePtr)
  20. {
  21.         int Status;

  22.         Status = XIntc_Initialize(IntcInstancePtr, INTC_DEVICE_ID);
  23.                 if (Status != XST_SUCCESS) {
  24.                         return XST_FAILURE;
  25.                 }

  26.         return XST_SUCCESS;
  27. }

  28. void Intc_Enable(XIntc *IntcInstancePtr,u16 Intr_Id)
  29. {
  30.         XIntc_Enable(IntcInstancePtr, Intr_Id);
  31. }

  32. int Intc_Start(XIntc *IntcInstancePtr)
  33. {
  34.         int Status = XIntc_Start(IntcInstancePtr, XIN_REAL_MODE);
  35.         if(Status != XST_SUCCESS){
  36.                 return XST_FAILURE;
  37.         }

  38.         return XST_SUCCESS;
  39. }
复制代码


本帖子中包含更多资源

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

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



Archiver|手机版|小黑屋|risc-v中文社区

GMT+8, 2024-4-20 20:02 , Processed in 0.025597 second(s), 18 queries .

risc-v中文社区论坛 官方网站

Copyright © 2018-2021, risc-v open source

快速回复 返回顶部 返回列表