当前位置: 首页 > 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库文件中定义完成,直接复制过来即可,在这个函数中首先判断终端的来源,之后通过调用发送函数原路径发出,当然这只是一个实验,功能比较简单,实际使用过程中切忌这种用法。

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

下板验证

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

​​​​​​​

相关文章:

  • 学做网站的视频教学如何在百度投放广告
  • 上海做设计公司网站seo是对网站进行什么优化
  • 购物网站建设 属于信息系统管理与设计么?成都seo经理
  • 大型购物网站建设方案电脑优化工具
  • 类似全民互推的推广平台福州seo网站推广优化
  • 武汉做网站网络公司网站点击量与排名
  • ELK服务搭建-0-1搭建记录
  • [ACTF新生赛2020]easyre
  • 分词算法BPE详解和CLIP的应用
  • Springboot怎么解决循环依赖
  • 针对vue项目的webpack优化攻略
  • 字节跳动2025年校招笔试手撕真题教程(二)
  • 神经网络学习-Day35
  • C语言指针进阶:通过地址,直接修改变量的值
  • 基于Python的全卷积网络(FCN)实现路径损耗预测
  • 为什么hash函数能减少哈希冲突
  • 内存管理 : 03多级页表和快表
  • 简单血条于小怪攻击模板
  • 开源项目跨平台桌宠 BongoCat,为桌面增添乐趣!
  • Java文件操作:从“Hello World”到“Hello File”
  • 打卡第28天:装饰器
  • 数据结构第2章绪论 (竟成)
  • CVE-2017-5645源码分析与漏洞复现(反序列化)
  • P1104 生日
  • go1.24 通过汇编深入学习map引入swiss table后的源码
  • MySQL | 比特BIT类型的使用指南