设置Uart中断处理程序
/*
* Setup the handlers for the UART that will be called from the
* interrupt context when data has been sent and received, specify
* a pointer to the UART driver instance as the callback reference
* so the handlers are able to access the instance data
*/
XUartPs_SetHandler(UartInstPtr, (XUartPs_Handler)Handler, UartInstPtr);
中断掩码:
/*
* Enable the interrupt of the UART so interrupts will occur, setup
* a local loopback so data that is sent will be received.
*/
IntrMask =
XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY | XUARTPS_IXR_FRAMING |
XUARTPS_IXR_OVER | XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXFULL |
XUARTPS_IXR_RXOVR;
#define XUARTPS_IXR_RBRK 0x00002000U /**< Rx FIFO 中断检测中断 */
#define XUARTPS_IXR_TOVR 0x00001000U /**< Tx FIFO 溢出中断 */
#define XUARTPS_IXR_TNFUL 0x00000800U /**< Tx FIFO 近满中断 */
#define XUARTPS_IXR_TTRIG 0x 00000400U /* *< Tx Trig 中断 */
#define XUARTPS_IXR_DMS 0x00000200U /**< 调制解调器状态更改中断 */
#define XUARTPS_IXR_TOUT 0x00000100U /**< 超时错误中断 */
#define XUARTPS_IXR_PARITY 0x00000080U /**< 奇偶校验错误中断 */
#define XUARTPS_IXR_FRAMING 0x00000040U /**< 帧错误中断 */
#define XUARTPS_IXR_OVER 0x00000020U /**< 溢出错误中断 */
#define XUARTPS_IXR_TXFULL 0x00000010U /**< TX FIFO 满中断。 */
#define XUARTPS_IXR_TXEMPTY 0x00000008U /**< TX FIFO 空中断。 */
#define XUARTPS_IXR_RXFULL 0x00000004U /**< RX FIFO 满中断。 */
#define XUARTPS_IXR_RXEMPTY 0x00000002U /**< RX FIFO 空中断。 */
#define XUARTPS_IXR_RXOVR 0x00000001U /**< RX FIFO 触发中断。 */
#define XUARTPS_IXR_MASK 0x00003FFFU /**< 有效位掩码 */
void Uart_SetHandler_Event(){
u32 IntrMask = 0;
int Index;
XUartPs_SetHandler(&Uart_Instance_point,
(XUartPs_Handler) Uart0_IntrHandler,
&Uart_Instance_point);
IntrMask = XUARTPS_IXR_TOUT|XUARTPS_IXR_RXOVR;
XUartPs_SetInterruptMask(&Uart_Instance_point, IntrMask);
XUartPs_SetOperMode(&Uart_Instance_point,XUARTPS_OPER_MODE_NORMAL);
XUartPs_SetRecvTimeout(&Uart_Instance_point,8);
// XUartPs_SetFifoThreshold(&Uart_Instance_point,8);
// Clear Buffer
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
SendBuffer[Index] = 0;
RecvBuffer[Index] = 0;
}
XUartPs_Recv(&Uart_Instance_point, RecvBuffer, TEST_BUFFER_SIZE);
// XUartPs_Send(&Uart_Instance_point, SendBuffer, TEST_BUFFER_SIZE);
}
/*
* Setup the handlers for the UART that will be called from the
* interrupt context when data has been sent and received, specify
* a pointer to the UART driver instance as the callback reference
* so the handlers are able to access the instance data
*/
XUartPs_SetHandler(UartInstPtr, (XUartPs_Handler)Handler, UartInstPtr);
/*
* Enable the interrupt of the UART so interrupts will occur, setup
* a local loopback so data that is sent will be received.
*/
IntrMask =
XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY | XUARTPS_IXR_FRAMING |
XUARTPS_IXR_OVER | XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXFULL |
XUARTPS_IXR_RXOVR;
if (UartInstPtr->Platform == XPLAT_ZYNQ_ULTRA_MP) {
IntrMask |= XUARTPS_IXR_RBRK;
}
XUartPs_SetInterruptMask(UartInstPtr, IntrMask);
XUartPs_SetOperMode(UartInstPtr, XUARTPS_OPER_MODE_LOCAL_LOOP);
/*
* Set the receiver timeout. If it is not set, and the last few bytes
* of data do not trigger the over-water or full interrupt, the bytes
* will not be received. By default it is disabled.
*
* The setting of 8 will timeout after 8 x 4 = 32 character times.
* Increase the time out value if baud rate is high, decrease it if
* baud rate is low.
*/
XUartPs_SetRecvTimeout(UartInstPtr, 8);
中断函数本身
void Handler(void *CallBackRef, u32 Event, unsigned int EventData)
{
/* All of the data has been sent */
if (Event == XUARTPS_EVENT_SENT_DATA) {
TotalSentCount = EventData;
}
/* All of the data has been received */
if (Event == XUARTPS_EVENT_RECV_DATA) {
TotalReceivedCount = EventData;
}
/*
* Data was received, but not the expected number of bytes, a
* timeout just indicates the data stopped for 8 character times
*/
if (Event == XUARTPS_EVENT_RECV_TOUT) {
TotalReceivedCount = EventData;
}
/*
* Data was received with an error, keep the data but determine
* what kind of errors occurred
*/
if (Event == XUARTPS_EVENT_RECV_ERROR) {
TotalReceivedCount = EventData;
TotalErrorCount++;
}
/*
* Data was received with an parity or frame or break error, keep the data
* but determine what kind of errors occurred. Specific to Zynq Ultrascale+
* MP.
*/
if (Event == XUARTPS_EVENT_PARE_FRAME_BRKE) {
TotalReceivedCount = EventData;
TotalErrorCount++;
}
/*
* Data was received with an overrun error, keep the data but determine
* what kind of errors occurred. Specific to Zynq Ultrascale+ MP.
*/
if (Event == XUARTPS_EVENT_RECV_ORERR) {
TotalReceivedCount = EventData;
TotalErrorCount++;
}
}