Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- structural modeling
- BASYS3
- soc 설계
- hc-sr04
- LED
- behavioral modeling
- pwm
- test bench
- verilog
- java
- atmega 128a
- KEYPAD
- FND
- DHT11
- vivado
- Pspice
- Edge Detector
- uart 통신
- gpio
- stop watch
- Recursion
- half adder
- i2c 통신
- D Flip Flop
- ATMEGA128A
- Algorithm
- prescaling
- ring counter
- Linked List
- dataflow modeling
Archives
- Today
- Total
거북이처럼 천천히
xuartlite_intr_example.c 분석 본문
1. xuartlite_intr_example.c
/******************************************************************************
*
* Copyright (C) 2002 - 2015 Xilinx, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/
/******************************************************************************/
/**
*
* @file xuartlite_intr_example.c
*
* This file contains a design example using the UartLite driver (XUartLite) and
* hardware device using the interrupt mode.
*
* @note
*
* The user must provide a physical loopback such that data which is
* transmitted will be received.
*
* MODIFICATION HISTORY:
* <pre>
* Ver Who Date Changes
* ----- ---- -------- -----------------------------------------------
* 1.00a jhl 02/13/02 First release
* 1.00b rpm 10/01/03 Made XIntc declaration global
* 1.00b sv 06/09/05 Minor changes to comply to Doxygen and coding guidelines
* 2.00a ktn 10/20/09 Updated to use HAL Processor APIs and minor changes
* for coding guidelnes.
* 3.2 ms 01/23/17 Added xil_printf statement in main function to
* ensure that "Successfully ran" and "Failed" strings
* are available in all examples. This is a fix for
* CR-965028.
* </pre>
******************************************************************************/
/***************************** Include Files *********************************/
#include "xparameters.h"
#include "xuartlite.h"
#include "xintc.h"
#include "xil_exception.h"
#include "xil_printf.h"
/************************** Constant Definitions *****************************/
/*
* The following constants map to the XPAR parameters created in the
* xparameters.h file. They are defined here such that a user can easily
* change all the needed parameters in one place.
*/
#define UARTLITE_DEVICE_ID XPAR_UARTLITE_0_DEVICE_ID
#define INTC_DEVICE_ID XPAR_INTC_0_DEVICE_ID
#define UARTLITE_INT_IRQ_ID XPAR_INTC_0_UARTLITE_0_VEC_ID
/*
* The following constant controls the length of the buffers to be sent
* and received with the UartLite device.
*/
#define TEST_BUFFER_SIZE 100
/**************************** Type Definitions *******************************/
/***************** Macros (Inline Functions) Definitions *********************/
/************************** Function Prototypes ******************************/
int UartLiteIntrExample(u16 DeviceId);
int SetupInterruptSystem(XUartLite *UartLitePtr);
void SendHandler(void *CallBackRef, unsigned int EventData);
void RecvHandler(void *CallBackRef, unsigned int EventData);
/************************** Variable Definitions *****************************/
XUartLite UartLite; /* The instance of the UartLite Device */
XIntc InterruptController; /* The instance of the Interrupt Controller */
/*
* The following variables are shared between non-interrupt processing and
* interrupt processing such that they must be global.
*/
/*
* The following buffers are used in this example to send and receive data
* with the UartLite.
*/
u8 SendBuffer[TEST_BUFFER_SIZE];
u8 ReceiveBuffer[TEST_BUFFER_SIZE];
/*
* The following counters are used to determine when the entire buffer has
* been sent and received.
*/
static volatile int TotalReceivedCount;
static volatile int TotalSentCount;
/******************************************************************************/
/**
*
* Main function to call the UartLite interrupt example.
*
* @param None
*
* @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful
*
* @note None
*
*******************************************************************************/
int main_UART_Interrupt(void)
{
int Status;
/*
* Run the UartLite Interrupt example, specify the Device ID that is
* generated in xparameters.h.
*/
Status = UartLiteIntrExample(UARTLITE_DEVICE_ID);
if (Status != XST_SUCCESS) {
xil_printf("Uartlite interrupt Example Failed\r\n");
return XST_FAILURE;
}
xil_printf("Successfully ran Uartlite interrupt Example\r\n");
return XST_SUCCESS;
}
/****************************************************************************/
/**
*
* This function does a minimal test on the UartLite device and driver as a
* design example. The purpose of this function is to illustrate
* how to use the XUartLite component.
*
* This function sends data and expects to receive the same data through the
* UartLite. The user must provide a physical loopback such that data which is
* transmitted will be received.
*
* This function uses interrupt driver mode of the UartLite device. The calls
* to the UartLite driver in the handlers should only use the non-blocking
* calls.
*
* @param DeviceId is the Device ID of the UartLite Device and is the
* XPAR_<uartlite_instance>_DEVICE_ID value from xparameters.h.
*
* @return XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note
*
* This function contains an infinite loop such that if interrupts are not
* working it may never return.
*
****************************************************************************/
int UartLiteIntrExample(u16 DeviceId)
{
int Status;
int Index;
/*
* Initialize the UartLite driver so that it's ready to use.
*/
Status = XUartLite_Initialize(&UartLite, DeviceId);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Perform a self-test to ensure that the hardware was built correctly.
*/
Status = XUartLite_SelfTest(&UartLite);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Connect the UartLite to the interrupt subsystem such that interrupts can
* occur. This function is application specific.
*/
Status = SetupInterruptSystem(&UartLite);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Setup the handlers for the UartLite that will be called from the
* interrupt context when data has been sent and received, specify a
* pointer to the UartLite driver instance as the callback reference so
* that the handlers are able to access the instance data.
*/
XUartLite_SetSendHandler(&UartLite, SendHandler, &UartLite);
XUartLite_SetRecvHandler(&UartLite, RecvHandler, &UartLite);
/*
* Enable the interrupt of the UartLite so that interrupts will occur.
*/
XUartLite_EnableInterrupt(&UartLite);
/*
* Initialize the send buffer bytes with a pattern to send and the
* the receive buffer bytes to zero to allow the receive data to be
* verified.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
SendBuffer[Index] = Index;
ReceiveBuffer[Index] = 0;
}
/*
* Start receiving data before sending it since there is a loopback.
*/
XUartLite_Recv(&UartLite, ReceiveBuffer, TEST_BUFFER_SIZE);
/*
* Send the buffer using the UartLite.
*/
XUartLite_Send(&UartLite, SendBuffer, TEST_BUFFER_SIZE);
/*
* Wait for the entire buffer to be received, letting the interrupt
* processing work in the background, this function may get locked
* up in this loop if the interrupts are not working correctly.
*/
while ((TotalReceivedCount != TEST_BUFFER_SIZE) ||
(TotalSentCount != TEST_BUFFER_SIZE)) {
}
/*
* Verify the entire receive buffer was successfully received.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
if (ReceiveBuffer[Index] != SendBuffer[Index]) {
return XST_FAILURE;
}
}
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* This function is the handler which performs processing to send data to the
* UartLite. It is called from an interrupt context such that the amount of
* processing performed should be minimized. It is called when the transmit
* FIFO of the UartLite is empty and more data can be sent through the UartLite.
*
* This handler provides an example of how to handle data for the UartLite,
* but is application specific.
*
* @param CallBackRef contains a callback reference from the driver.
* In this case it is the instance pointer for the UartLite driver.
* @param EventData contains the number of bytes sent or received for sent
* and receive events.
*
* @return None.
*
* @note None.
*
****************************************************************************/
void SendHandler(void *CallBackRef, unsigned int EventData)
{
TotalSentCount = EventData;
}
/****************************************************************************/
/**
*
* This function is the handler which performs processing to receive data from
* the UartLite. It is called from an interrupt context such that the amount of
* processing performed should be minimized. It is called data is present in
* the receive FIFO of the UartLite such that the data can be retrieved from
* the UartLite. The size of the data present in the FIFO is not known when
* this function is called.
*
* This handler provides an example of how to handle data for the UartLite,
* but is application specific.
*
* @param CallBackRef contains a callback reference from the driver, in
* this case it is the instance pointer for the UartLite driver.
* @param EventData contains the number of bytes sent or received for sent
* and receive events.
*
* @return None.
*
* @note None.
*
****************************************************************************/
void RecvHandler(void *CallBackRef, unsigned int EventData)
{
TotalReceivedCount = EventData;
}
/****************************************************************************/
/**
*
* This function setups the interrupt system such that interrupts can occur
* for the UartLite device. This function is application specific since the
* actual system may or may not have an interrupt controller. The UartLite
* could be directly connected to a processor without an interrupt controller.
* The user should modify this function to fit the application.
*
* @param UartLitePtr contains a pointer to the instance of the UartLite
* component which is going to be connected to the interrupt
* controller.
*
* @return XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note None.
*
****************************************************************************/
int SetupInterruptSystem(XUartLite *UartLitePtr)
{
int Status;
/*
* Initialize the interrupt controller driver so that it is ready to
* use.
*/
Status = XIntc_Initialize(&InterruptController, INTC_DEVICE_ID);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Connect a device driver handler that will be called when an interrupt
* for the device occurs, the device driver handler performs the
* specific interrupt processing for the device.
*/
Status = XIntc_Connect(&InterruptController, UARTLITE_INT_IRQ_ID,
(XInterruptHandler)XUartLite_InterruptHandler,
(void *)UartLitePtr);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Start the interrupt controller such that interrupts are enabled for
* all devices that cause interrupts, specific real mode so that
* the UartLite can cause interrupts through the interrupt controller.
*/
Status = XIntc_Start(&InterruptController, XIN_REAL_MODE);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Enable the interrupt for the UartLite device.
*/
XIntc_Enable(&InterruptController, UARTLITE_INT_IRQ_ID);
/*
* Initialize the exception table.
*/
Xil_ExceptionInit();
/*
* Register the interrupt controller handler with the exception table.
*/
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XIntc_InterruptHandler,
&InterruptController);
/*
* Enable exceptions.
*/
Xil_ExceptionEnable();
return XST_SUCCESS;
}
2. 각각의 변수의 역활 및 의미
2.1) UARTLITE_DEVICE_ID (= XPAR_UARTLITE_0_DEVICE_ID)
#define UARTLITE_DEVICE_ID XPAR_UARTLITE_0_DEVICE_ID
- FPGA 시스템내에 특정 모듈인 UARTLite 모듈을 식별하기 위한 Device ID
2.2) UartLite
/************************** Variable Definitions *****************************/
XUartLite UartLite; /* The instance of the UartLite Device */
- UARTLite 장치에 대한 드라이버를 나타내는 구조체의 인스턴스
- 인스턴스를 통해 UART 모듈을 초기화하고 설정하거나 데이터를 전송/수신하는 등의 제어 작업을 수행 가능
2.3) InterruptController
XIntc InterruptController; /* The instance of the Interrupt Controller */
- Interrupt Controller 모듈에 대한 구조체의 인스턴스
- 해당 인스턴스를 통해 Interrupt Controller 모듈을 초기화하고, 설정 등의 제어 작업을 수행 가능
2.4) UARTLITE_INT_IRQ_ID (= XPAR_INTC_0_UARTLITE_0_VEC_ID)
#define UARTLITE_INT_IRQ_ID XPAR_INTC_0_UARTLITE_0_VEC_ID
- 해당 Vector ID는 Interrupt Controller에게 있어 다양한 Interrupt Vecotr ID 중에서 "해당 Interrupt가 어떠한 모듈로부터 전송되었는가?"를 나타내게 한다.
- 따라서 해당 XPAR_INTC_0_UARTLITE_0_VEC_ID 를 통해 수많은 Vector ID 중에서 UART 모듈로부터 발생했음을 식별할 수 있는 것이다.
3. 각 함수의 역활
3.1) XIntc_Connect
/*
* Connect a device driver handler that will be called when an interrupt
* for the device occurs, the device driver handler performs the
* specific interrupt processing for the device.
*/
Status = XIntc_Connect(&InterruptController, UARTLITE_INT_IRQ_ID,
(XInterruptHandler)XUartLite_InterruptHandler,
(void *)UartLitePtr);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
- InterruptController 인스턴스를 통해 Interrupt Controller와 UART Interrupt Vector ID를 연결한다.
- UART Interrupt 발생시 "XUartLite_InterruptHandler"를 실행시키도록 해당 Handler를 Interrupt Handler로 설정
- Interrupt 처리 완료시 "UartLitePtr"를 Callback 함수로서 설정
< 요약 정리 > - Interrupt Controller가 UARTLITE_INT_IRQ_ID로 식별되는 UART 인터럽트와 연결됩니다.
- UART 인터럽트 발생 시, XUartLite_InterruptHandler가 호출되어 인터럽트 처리를 수행합니다.
- 인터럽트 핸들러는 UART 인스턴스(UartLitePtr)를 사용하여 적절한 작업(예: 데이터 송/수신)을 실행합니다.
3.2) Xil_ExceptionRegisterHandler
/*
* Register the interrupt controller handler with the exception table.
*/
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XIntc_InterruptHandler,
&InterruptController);
- Interrupt Controller에서 예외처리가 발생할 경우, XIntc_InterruptHandler를 통해 예외처리를 실행한다.
3.3) XUartLite_SetSendHandler, XUartLite_SetRecvHandler
/*
* Setup the handlers for the UartLite that will be called from the
* interrupt context when data has been sent and received, specify a
* pointer to the UartLite driver instance as the callback reference so
* that the handlers are able to access the instance data.
*/
XUartLite_SetSendHandler(&UartLite, SendHandler, &UartLite);
XUartLite_SetRecvHandler(&UartLite, RecvHandler, &UartLite);
- UART 송신 완료시 발생하는 Interrupt에 대한 Handler로서 SendHandler를 등록
- UART 수신 완료시 발생하는 Interrupt에 대한 Handler로서 RecHandler를 등록
★★★★★★★★★★★★★★★ 중요 ★★★★★★★★★★★★★★★
Q) XUartLite_SetSendHandler와 XUartLite_SetRecvHandler 함수가 각각 UART통신에서 데이터 송신완료, 수신완료시 발생하는 Interrupt에 대한 Handler를 등록하는 것을 이해했다. 근데, SetupInterruptSystem 함수에서 XIntc_Connect 함수 선언시, XUartLite_InterruptHandler 라는 헨들러를 등록했는데, 왜 추가적으로 UART Interrupt에 대한 Handler를 등록하는 것인가?
이는 또 다른 Handler를 등록함으로서 UART Interrupt 발생한 부분에 대해서 다수의 Handler가 동작함으로서 충돌이 발생하는 것이 아닌가?
A)
핸들러의 역할 차이
- XUartLite_InterruptHandler (Interrupt Controller 핸들러)
- 이 핸들러는 인터럽트 발생 시 호출되는 함수로, UART 장치에서 발생한 인터럽트를 관리하는 역할을 합니다.
- 이 핸들러는 UART 모듈의 상태를 확인하고, 송신 또는 수신 이벤트가 발생했는지 판단합니다.
- 따라서, 이 핸들러는 UART의 상태에 따라 적절한 송신 핸들러 또는 수신 핸들러를 호출하게 됩니다.
- SendHandler 및 RecvHandler (UART 드라이버 핸들러)
- 이 핸들러들은 UART 모듈에서 송신 또는 수신 완료 이벤트가 발생했을 때 실행되는 함수입니다.
- 각각의 핸들러는 UART의 송신 FIFO가 비었을 때 데이터를 전송하거나, 수신 FIFO에 데이터가 있을 때 이를 처리하는 등의 작업을 수행합니다.
작동 흐름
- Interrupt 발생: UART에서 데이터 송신 또는 수신 이벤트가 발생하면,
- Interrupt Controller는 먼저 XUartLite_InterruptHandler를 호출합니다.
- 이 핸들러는 어떤 종류의 인터럽트가 발생했는지를 판별합니다.
- 예를 들어, 송신이 완료되었다면 SendHandler를 호출하고, 수신이 완료되었다면 RecvHandler를 호출합니다.
핸들러 구조 요약
- XIntc_Connect는 UART 인터럽트가 발생했을 때의 진입점인 상위 핸들러를 등록합니다.
- XUartLite_SetSendHandler와 XUartLite_SetRecvHandler는 각각 송신 및 수신 이벤트에 대한 하위 핸들러를 등록합니다.
- 중복되지 않음: 핸들러는 서로 다른 역할을 수행하며, XUartLite_InterruptHandler는 인터럽트를 관리하는 통합 핸들러이고, SendHandler 및 RecvHandler는 송신 및 수신 작업을 수행합니다.
UART Interrupt에 대한 처리 흐름도
'FPGA 정리 > Xilinx SDK Example 분석' 카테고리의 다른 글
xspi_slave_intr_example.c 분석 (0) | 2025.01.15 |
---|---|
xintc_example.c 분석 (0) | 2025.01.15 |