zynq SDK CAN中断接收数据例程
时间: 2023-06-22 13:37:33 浏览: 230
ZYNQ PS响应PL中断例程
以下一个简单的Zynq SDK CAN中断接收数据的例程,仅供参考:
```c
#include "xparameters.h"
#include "xscugic.h"
#include "xil_exception.h"
#include "xcan.h"
#define CAN_DEVICE_ID XPAR_CAN_0_DEVICE_ID
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID
#define CAN_INT_ID XPAR_INTC_0_CAN_0_VEC_ID
#define INTC XScuGic
#define CAN XCan
#define CAN_BUFFER_SIZE 8
static CAN CanInstance;
static INTC IntcInstance;
static XScuGic_Config *IntcConfig;
static u8 CanRxBuffer[CAN_BUFFER_SIZE];
static u32 CanRxBufferIndex = 0;
static void CanIntrHandler(void *CallbackRef)
{
u32 Status;
CAN *CanPtr = (CAN *)CallbackRef;
Status = XCan_GetStatus(CanPtr);
if (Status & XCAN_SR_RXNEMPTY_MASK) {
u32 RxFrameSize;
XCan_ReadRecvFrame(CanPtr, CanRxBuffer + CanRxBufferIndex, &RxFrameSize);
CanRxBufferIndex += RxFrameSize;
}
XCan_ClearInterrupt(CanPtr, Status);
}
static int SetupInterruptSystem(CAN *CanPtr, INTC *IntcPtr)
{
XScuGic_Config *IntcConfig;
int Status;
IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
Status = XScuGic_CfgInitialize(IntcPtr, IntcConfig, IntcConfig->CpuBaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler,
IntcPtr);
Xil_ExceptionEnable();
XScuGic_Connect(IntcPtr,
CAN_INT_ID,
(Xil_ExceptionHandler)CanIntrHandler,
(void *)CanPtr);
XScuGic_Enable(IntcPtr, CAN_INT_ID);
return XST_SUCCESS;
}
int main()
{
int Status;
XCan_Config *CanConfigPtr;
XCan *CanInstancePtr = &CanInstance;
CanConfigPtr = XCan_LookupConfig(CAN_DEVICE_ID);
if (CanConfigPtr == NULL) {
return XST_FAILURE;
}
Status = XCan_CfgInitialize(CanInstancePtr, CanConfigPtr, CanConfigPtr->BaseAddr);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
XCan_SetMode(CanInstancePtr, XCAN_MODE_CONFIG);
XCan_SetBaudRatePrescaler(CanInstancePtr, 0, 5);
XCan_SetMode(CanInstancePtr, XCAN_MODE_NORMAL);
Status = SetupInterruptSystem(CanInstancePtr, &IntcInstance);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
XCan_InterruptEnable(CanInstancePtr, XCAN_IXR_RXNEMP_MASK);
XCan_InterruptGlobalEnable(CanInstancePtr);
XCan_Start(CanInstancePtr);
while (1) {
// Do something with received data
}
return 0;
}
```
这个例程初始化了CAN控制器,并使用中断方式接收CAN总线上的数据。在中断处理函数CanIntrHandler中,将接收到的数据保存到CanRxBuffer中,并在主程序中进行处理。注意,在主程序中需要使用Xil_ExceptionEnable函数使能异常处理。
阅读全文