平臺中斷控制器(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文件中,我們找到如下函數:
- / *
- Function: PLIC_TrapHandler
- Description: Interrupt handler type selection.
- Param: Mcause: determine the type of exception or interrupt based on the value of the mcause register.
- Return: None
- */
- void PLIC_TrapHandler(uint32_t Mcause)
- {
*/* Initializes the external interrupt structure */*
PLIC_EXTITypeDef ExtInt = {{0}, 0};
11.
12. ```
if((Mcause & 0x80000000) != 0)
{
14. ```
switch (Mcause & 0x0fff)
{
16. ```
case 3: */* Machine software interrupt */*
MSoftWare_IRQ_Handler();
18. ```
break;
case 7: / Machine timer interrupt /
20. ```
MTimer_IRQ_Handler();
break;
22. ```
case 11: */* Machine external interrupt */*
PLIC_SwitchMEXTI(&ExtInt);
24. ```
break;
default:
26. ```
break;
}
28. ```
}
else
30. ```
{
switch (Mcause & 0xfff)
32. ```
{
case 0: / Instruction address misaligned /
34. ```
InstAddrMisalign_Handler();
break;
36. ```
case 1: */* Instruction access fault */*
InstAccessFault_Handler();
38. ```
break;
case 2: / Illegal instruction /
40. ```
IllegalInst_Handler();
break;
42. ```
case 3: */* Breakpoint */*
Breakpoint_Handler();
44. ```
break;
case 4: / Load address misaligned /
46. ```
LoadAddrMisalign_Handler();
break;
48. ```
case 5: */* Load access fault */*
LoadAccessFault_Handler();
50. ```
break;
case 6: / Store/AMO address misaligned /
52. ```
StoreAMOAddrMisalign_Handler();
break;
54. ```
case 7: */* Store/AMO access fault */*
StoreAMOAccessFault_Handler();
56. ```
break;
case 11: / Environment call from M-mode /
58. ```
ECall_Handler();
break;
60. ```
case 12: */* Instruction page fault */*
InstPageFault_Handler();
62. ```
break;
case 13: / Load page fault /
64. ```
LoadPageFault_Handler();
break;
66. ```
case 15: */* Store/AMO page fault */*
StoreAMOPageFalut_Handler();
68. ```
break;
default:
70. ```
break;
}
72. ```
}
- }
在這個函數中,系統中斷首先會讀取MCAUSE寄存器的最高位,如果最高位為0,代表此事件為異常,RISCV定義了此類型,具體可直接查看MCAUSE寄存器定義;如果最高位為1,證明此事件為系統中斷,此時可根據低位去選擇處理的中斷類型。
AS32除了系統定時中斷和軟件中斷外,plic定義了64個plic中斷,之前的的異常和中斷均為向量類型,但進入plic中斷后即為非向量模式,但可以軟件支持嵌套,64個中斷類型均已經在此文件中定義,所有定義均為弱函數,因此可以復制中斷處理函數名寫在自定義位置。接下來以串口中斷為例介紹用法:
復制之前的usart工程,在print.c中修改初始化代碼如下:
- / *
- Function: User_Print_Init
- Description: Configure Print USART.
- Param: BaudRate: USART communication baud rate.
- Return: None.
- */
- void User_Print_Init(uint32_t BaudRate)
- {
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
11. ```
PLIC_InitTypeDef PLIC_InitStructure;
GPIOD_CLK_ENABLE();
14. ```
USART0_CLK_ENABLE();
/ Set GPIO multiplex mapping /
17. ```
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART0); */* USART0_TX */*
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART0); / USART0_RX /
19. ```
*/* GPIO Configure */*
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
21. ```
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_Out_PP;
23. ```
GPIO_InitStructure.GPIO_OStrength = GPIO_OStrength_4_5mA;
GPIO_Init(GPIOD, &GPIO_InitStructure);
25. ```
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
27. ```
GPIO_InitStructure.GPIO_IType = GPIO_IN_FLOATING;
GPIO_InitStructure.GPIO_OStrength = GPIO_OStrength_4_5mA;
29. ```
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_DeInit(USART0);
32.
33. ```
USART_StructInit(&USART_InitStructure);
/ Initializes the USART0 /
36. ```
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
38. ```
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
40. ```
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_OverSampling = USART_OverSampling_16;
42. ```
USART_Init(USART0, &USART_InitStructure);
USART_Cmd(USART0, ENABLE);
45.
46. ```
USART_ITConfig(USART0, USART_IT_RXNE, ENABLE);
/ Configer the USART0 interrupt /
49. ```
PLIC_InitStructure.PLIC_IRQChannel = USART0_IRQn;
PLIC_InitStructure.PLIC_IRQPriority = 1;
51. ```
PLIC_InitStructure.PLIC_IRQChannelCmd = ENABLE;
PLIC_Init(&PLIC_InitStructure);
53. }
54.
55. */* *
56. * Function: USART0_IRQ_Handler
57. * Description: USART0 interrupt handler function.
58. * Param: None.
59. * Return: None.
60. */
61.
62. void USART0_IRQ_Handler()
63. {
64.
65. ```
if(USART_GetFlagStatus(USART0, USART_FLAG_RXNE) != RESET)
{
67. ```
*/* Clear the interrupt pending bits */*
USART_SendData(USART0,USART_ReceiveData(USART0));
69. ```
}
- }
在這個代碼中,44行之前和串口章節完全一樣,不再重復進行說明。第46行,調用串口的中斷使能函數,使能串口接收中斷,該處形參中的中斷類型已經定義好,可以自行查詢,之后需要開啟PLIC的中斷通道以及優先級配置,之后調用PLIC_Init函數進行初始化。
接下來,需要重寫中斷處理函數,該函數名已經在PLIC庫文件中定義完成,直接復制過來即可,在這個函數中首先判斷終端的來源,之后通過調用發送函數原路徑發出,當然這只是一個實驗,功能比較簡單,實際使用過程中切忌這種用法。
最后主函數中對上述代碼只需要做初始化即可,沒有實際邏輯,因此在這不做展示。
下板驗證
將上述代碼編譯燒錄完成,連接串口線與上位機,觀察現象。
審核編輯 黃宇
-
驅動
+關注
關注
12文章
1914瀏覽量
86834 -
中斷控制器
+關注
關注
0文章
61瀏覽量
9665
發布評論請先 登錄
AS32X601驅動系列教程 USART_串口通訊詳解

AS32X601驅動系列教程 GPIO_按鍵檢測詳解

AS32X601驅動系列教程 GPIO_點亮LED詳解

AS32X601驅動系列教程 SMU_系統時鐘詳解

新品 | EiceDRIVER? 650 V +/- 4 A高壓側柵極驅動器 1ED21x7 系列

面向工業與汽車領域的高安全可靠MCU——AS32X601系列芯片解析
AS32X601芯片技術剖析
AS32X601雙核鎖步MCU技術優勢分析
單片機中斷技術詳解
請問關于DSP2802x,2803x或者2806x的AD轉換模塊中斷的問題
EE-188:使用C語言在ADSP-219x DSP上實現中斷驅動系統

TMS320x280x、2801x、2804x DSP系統控制和中斷參考指南

評論