当前位置: 首页 > news >正文

【AS32X601驱动系列教程】PLIC_中断应用详解

平台中断控制器(Platform Level Interrupt Controller,PLIC)是国科安芯AS32系列MCU芯片的中断控制器,主要对中断源进行采样,优先级仲裁和分发。各外设中断统一连到PLIC,PLIC统一管理并输出中断请求到内核。

硬件设计

本节硬件同USART章节一致。

软件设计

代码分析

在之前的按键章节我们已经对AS32的中断进行了简单实用,本节将用串口的接收中断实验进一步加深一下使用过程。

回顾之前的启动文件章节,有如下一段代码:

在RISCV指令集中,在机器模式下中断相关的寄存器有MSTATUS、MIE和MTVEC,其中前两个寄存器控制系统中断使能,具体内容颗翻看启动文件讲解,MTVEC用于保存中断入口地址,当中断发生时,程序指针会自动跳转到TrapEntry地址处开始执行,该段代码位于as32x601_trapentry.S文件中,用汇编文件编写,在这个函数下,我们会将RISCV内核所有相关寄存器,包括PC指针等全部进行保存,然后调用中断入口处理函数,完成后恢复现场寄存器值,从而实现中断功能。

中断处理函数位于as32x601_plic.c文件中,我们找到如下函数:

  1. /*
  2.  * Function:        PLIC_TrapHandler
  3.  * Description:     Interrupt handler type selection.
  4.  * Param:           Mcause: determine the type of exception or interrupt based on the value of the mcause register.
  5.  * Return:          None
  6.  */
  7. void PLIC_TrapHandler(uint32_t Mcause)
  8. {
  9.     /* Initializes the external interrupt structure */
  10.     PLIC_EXTITypeDef ExtInt = {{0}, 0};
  11.     if((Mcause & 0x80000000) != 0)
  12.     {
  13.         switch (Mcause & 0x0fff
  14.         {
  15.             case 3/* Machine software interrupt */
  16.                 MSoftWare_IRQ_Handler();
  17.                 break;
  18.             case 7/* Machine timer interrupt */
  19.                 MTimer_IRQ_Handler();
  20.                 break;
  21.             case 11/* Machine external interrupt */
  22.                 PLIC_SwitchMEXTI(&ExtInt);
  23.                 break;
  24.             default:
  25.                 break;
  26.         }
  27.     }
  28.     else
  29.     {
  30.         switch (Mcause & 0xfff
  31.         {   
  32.             case 0/* Instruction address misaligned */
  33.                 InstAddrMisalign_Handler();
  34.                 break;
  35.             case 1/* Instruction access fault */
  36.                 InstAccessFault_Handler();
  37.                 break;
  38.             case 2/* Illegal instruction */
  39.                 IllegalInst_Handler();
  40.                 break;
  41.             case 3/* Breakpoint */
  42.                 Breakpoint_Handler();
  43.                 break;
  44.             case 4/* Load address misaligned */
  45.                 LoadAddrMisalign_Handler();
  46.                 break;
  47.             case 5/* Load access fault */
  48.                 LoadAccessFault_Handler();
  49.                 break;
  50.             case 6/* Store/AMO address misaligned */
  51.                 StoreAMOAddrMisalign_Handler();
  52.                 break;
  53.             case 7/* Store/AMO access fault */
  54.                 StoreAMOAccessFault_Handler();
  55.                 break;
  56.             case 11/* Environment call from M-mode */
  57.                 ECall_Handler();
  58.                 break;
  59.             case 12/* Instruction page fault */
  60.                 InstPageFault_Handler();
  61.                 break;
  62.             case 13/* Load page fault */
  63.                 LoadPageFault_Handler();
  64.                 break;
  65.             case 15/* Store/AMO page fault */
  66.                 StoreAMOPageFalut_Handler();
  67.                 break;
  68.             default:
  69.                 break;
  70.         }
  71.     }
  72. }

在这个函数中,系统中断首先会读取MCAUSE寄存器的最高位,如果最高位为0,代表此事件为异常,RISCV定义了此类型,具体可直接查看MCAUSE寄存器定义;如果最高位为1,证明此事件为系统中断,此时可根据低位去选择处理的中断类型。

AS32除了系统定时中断和软件中断外,plic定义了64个plic中断,之前的的异常和中断均为向量类型,但进入plic中断后即为非向量模式,但可以软件支持嵌套,64个中断类型均已经在此文件中定义,所有定义均为弱函数,因此可以复制中断处理函数名写在自定义位置。接下来以串口中断为例介绍用法:

复制之前的usart工程,在print.c中修改初始化代码如下:

  1. /*
  2.  * Function:        User_Print_Init
  3.  * Description:     Configure Print USART.
  4.  * Param:           BaudRate: USART communication baud rate.                 
  5.  * Return:          None.
  6.  */
  7. void User_Print_Init(uint32_t BaudRate)
  8. {
  9.     USART_InitTypeDef USART_InitStructure;
  10.     GPIO_InitTypeDef  GPIO_InitStructure;
  11.     PLIC_InitTypeDef PLIC_InitStructure;
  12.     
  13.     GPIOD_CLK_ENABLE();
  14.     USART0_CLK_ENABLE();
  15.         
  16.     /* Set GPIO multiplex mapping */
  17.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART0);       /* USART0_TX */
  18.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART0);       /* USART0_RX */
  19.     /* GPIO Configure */
  20.     GPIO_InitStructure.GPIO_Pin       = GPIO_Pin_8;             
  21.     GPIO_InitStructure.GPIO_Mode      = GPIO_Mode_OUT;
  22.     GPIO_InitStructure.GPIO_OType     = GPIO_Out_PP;
  23.     GPIO_InitStructure.GPIO_OStrength = GPIO_OStrength_4_5mA;
  24.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  25.     GPIO_InitStructure.GPIO_Pin       = GPIO_Pin_9;             
  26.     GPIO_InitStructure.GPIO_Mode      = GPIO_Mode_IN;
  27.     GPIO_InitStructure.GPIO_IType     = GPIO_IN_FLOATING;
  28.     GPIO_InitStructure.GPIO_OStrength = GPIO_OStrength_4_5mA;
  29.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  30.     USART_DeInit(USART0);
  31.     USART_StructInit(&USART_InitStructure);
  32.     /* Initializes the USART0 */
  33.     USART_InitStructure.USART_BaudRate     = BaudRate;
  34.     USART_InitStructure.USART_WordLength   = USART_WordLength_8b;
  35.     USART_InitStructure.USART_StopBits     = USART_StopBits_1;
  36.     USART_InitStructure.USART_Parity       = USART_Parity_No;
  37.     USART_InitStructure.USART_Mode         = USART_Mode_Rx | USART_Mode_Tx;
  38.     USART_InitStructure.USART_OverSampling = USART_OverSampling_16;
  39.     USART_Init(USART0, &USART_InitStructure);
  40.     USART_Cmd(USART0, ENABLE);
  41.     
  42.     USART_ITConfig(USART0, USART_IT_RXNE, ENABLE);
  43.     
  44.      /* Configer the USART0 interrupt */
  45.     PLIC_InitStructure.PLIC_IRQChannel = USART0_IRQn;
  46.     PLIC_InitStructure.PLIC_IRQPriority = 1;
  47.     PLIC_InitStructure.PLIC_IRQChannelCmd = ENABLE;
  48.     PLIC_Init(&PLIC_InitStructure);
  49. }
  50. /*
  51.  * Function:        USART0_IRQ_Handler
  52.  * Description:     USART0 interrupt handler function.
  53.  * Param:           None.                 
  54.  * Return:          None.
  55.  */
  56. void USART0_IRQ_Handler()
  57. {
  58.     
  59.     if(USART_GetFlagStatus(USART0, USART_FLAG_RXNE) != RESET)
  60.     {
  61.         /* Clear the interrupt pending bits */
  62.         USART_SendData(USART0,USART_ReceiveData(USART0));
  63.     }
  64. }

在这个代码中,44行之前和串口章节完全一样,不再重复进行说明。第46行,调用串口的中断使能函数,使能串口接收中断,该处形参中的中断类型已经定义好,可以自行查询,之后需要开启PLIC的中断通道以及优先级配置,之后调用PLIC_Init函数进行初始化。

接下来,需要重写中断处理函数,该函数名已经在PLIC库文件中定义完成,直接复制过来即可,在这个函数中首先判断终端的来源,之后通过调用发送函数原路径发出,当然这只是一个实验,功能比较简单,实际使用过程中切忌这种用法。

最后主函数中对上述代码只需要做初始化即可,没有实际逻辑,因此在这不做展示。

​​​​​​​下板验证

将上述代码编译烧录完成,连接串口线与上位机,观察现象。

​​​​​​​

http://www.xdnf.cn/news/619075.html

相关文章:

  • PADS LAYOUT添加GND过孔
  • 小豆包api:claude-sonnet-4,Claude 最新模型
  • 卖家受益于WOOT推广的逻辑
  • 基于QuestionPicture的图片批量处理方法与实践
  • 2025 ICPC 南昌全国邀请赛暨江西省赛(8题题解)
  • 三格电子上新了——高频工业 RFID 读写器
  • 理解网卡RSS
  • 深入理解会话管理:Cookie、Session与JWT的对比与应用
  • Python图像处理基础(四)
  • 信号与系统05-复频域分析(拉普拉斯变换与Z变换)
  • 飞书知识问答深度体验:企业AI应用落地的典范产品
  • 可解释性学习指标综述_Machine Learning Interpretability: A Survey on Methods and Metrics
  • unity在urp管线中插入事件
  • Opixs: Fluxim推出的全新显示仿真模拟软件
  • 易境通专线散拼系统:全方位支持多种专线物流业务!
  • 推挽电路工作原理及仿真
  • 从elf文件动态加载的过程解释got,plt及got.plt,plt.sec
  • JavaScript运算符全解析:从基础到进阶实战指南
  • 2025年中级社会工作者备考精选练习题
  • HardFault_Handler调试及问题方法
  • vue——v-pre的使用
  • Redis 面经
  • 开发指南118-背景渐变特效
  • SOC-ESP32S3部分:8-GPIO输出LED控制
  • 如何做好一份技术文档?
  • JavaSE核心知识点03高级特性03-01(集合框架)
  • AbMole| MG132(133407-82-6,M1902,蛋白酶体抑制剂)
  • 西北某县智慧水务系统新升级:能预判·会听话·秒响应的智能“水管家”上岗
  • 探索常识性概念图谱:构建智能生活的知识桥梁
  • YOLOv4论文超详细精讲(翻译+学习笔记)