基于FPGA的MB核下IIC的中断接收

tech2023-03-01  102

1、新建工程,如图所示 2、生成bit,导入sdk 3、c代码

#include "xparameters.h" #include "xiic.h" #include "xintc.h" #include "xil_exception.h" #include "xil_printf.h" #define IIC_DEVICE_ID XPAR_IIC_0_DEVICE_ID #define INTC_DEVICE_ID XPAR_INTC_0_DEVICE_ID #define IIC_INTR_ID XPAR_INTC_0_IIC_0_VEC_ID #define SLAVE_ADDRESS 0x38 /* 0x70 as PC TX ID. */ #define RECEIVE_COUNT 25 #define SEND_COUNT 25 int IicSlaveExample(); int SlaveWriteData(u16 ByteCount); int SlaveReadData(u8 *BufferPtr, u16 ByteCount); static int SetupInterruptSystem(XIic * IicInstPtr); static void StatusHandler(XIic *InstancePtr, int Event); static void SendHandler(XIic *InstancePtr); static void ReceiveHandler(XIic *InstancePtr); XIic IicInstance; XIntc InterruptController; u8 WriteBuffer[SEND_COUNT]; u8 ReadBuffer[RECEIVE_COUNT]; volatile u8 TransmitComplete; volatile u8 ReceiveComplete; volatile u8 SlaveRead; volatile u8 SlaveWrite; int main(void) { int Status; XIic_Config *ConfigPtr; /* Pointer to configuration data */ ConfigPtr = XIic_LookupConfig(IIC_DEVICE_ID); if (ConfigPtr == NULL) { return XST_FAILURE; } Status = XIic_CfgInitialize(&IicInstance, ConfigPtr,ConfigPtr->BaseAddress); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = SetupInterruptSystem(&IicInstance); if (Status != XST_SUCCESS) { return XST_FAILURE; } XIic_SlaveInclude(); XIic_SetStatusHandler(&IicInstance, &IicInstance, (XIic_StatusHandler) StatusHandler); XIic_SetSendHandler(&IicInstance, &IicInstance, (XIic_Handler) SendHandler); XIic_SetRecvHandler(&IicInstance, &IicInstance, (XIic_Handler) ReceiveHandler); Status = XIic_SetAddress(&IicInstance, XII_ADDR_TO_RESPOND_TYPE, SLAVE_ADDRESS); if (Status != XST_SUCCESS) { return XST_FAILURE; } while(1){ SlaveReadData(ReadBuffer, RECEIVE_COUNT); } } int SlaveReadData(u8 *BufferPtr, u16 ByteCount) { int Status; ReceiveComplete = 1; Status = XIic_Start(&IicInstance); if (Status != XST_SUCCESS) { return XST_FAILURE; } XIic_IntrGlobalEnable(IicInstance.BaseAddress); while ((ReceiveComplete) || (XIic_IsIicBusy(&IicInstance) == TRUE)) { if (SlaveRead) { XIic_SlaveRecv(&IicInstance, ReadBuffer, RECEIVE_COUNT); SlaveRead = 0; } } XIic_IntrGlobalDisable(IicInstance.BaseAddress); Status = XIic_Stop(&IicInstance); if (Status != XST_SUCCESS) { return XST_FAILURE; } return XST_SUCCESS; } int SlaveWriteData(u16 ByteCount) { int Status; TransmitComplete = 1; Status = XIic_Start(&IicInstance); if (Status != XST_SUCCESS) { return XST_FAILURE; } XIic_IntrGlobalEnable(IicInstance.BaseAddress); while ((TransmitComplete) || (XIic_IsIicBusy(&IicInstance) == TRUE)) { if (SlaveWrite) { XIic_SlaveSend(&IicInstance, WriteBuffer, SEND_COUNT); SlaveWrite = 0; } } XIic_IntrGlobalDisable(IicInstance.BaseAddress); Status = XIic_Stop(&IicInstance); if (Status != XST_SUCCESS) { return XST_FAILURE; } return XST_SUCCESS; } static void StatusHandler(XIic *InstancePtr, int Event) { if (Event == XII_MASTER_WRITE_EVENT) { SlaveRead = 1; } else { SlaveWrite = 1; } } static void SendHandler(XIic *InstancePtr) { TransmitComplete = 0; } static void ReceiveHandler(XIic *InstancePtr) { ReceiveComplete = 0; } static int SetupInterruptSystem(XIic * IicInstPtr) { int Status; if (InterruptController.IsStarted == XIL_COMPONENT_IS_STARTED) { return XST_SUCCESS; } Status = XIntc_Initialize(&InterruptController, INTC_DEVICE_ID); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = XIntc_Connect(&InterruptController, IIC_INTR_ID, (XInterruptHandler) XIic_InterruptHandler, IicInstPtr); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = XIntc_Start(&InterruptController, XIN_REAL_MODE); if (Status != XST_SUCCESS) { return XST_FAILURE; } XIntc_Enable(&InterruptController, IIC_INTR_ID); Xil_ExceptionInit(); Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler) XIntc_InterruptHandler, &InterruptController); Xil_ExceptionEnable(); return XST_SUCCESS; }

4、MASTER 发送ID设置为70,每次发送不超过25B,则发送任意长度数据,MICROBLAZE中断接收数据,数据保存在ReadBuffer[RECEIVE_COUNT]中。 5、资源连接https://download.csdn.net/download/sinat_39724439/12804804

最新回复(0)