#include <stdio.h>
#include "xparameters.h"
#include "xuartlite.h"
#include "xintc.h"
#include "xil_excep
tion.h"
#include "xgpio.h"
#include "xstatus.h"
#include "xgpio_l.h"
#include "xil_assert.h"
#include "xil_io.h"
#include "xuartlite_l.h"
/************************** Constant Definitions *****************************/
#define XPAR_GPIO_0_DEVICE_ID 1
#define UARTLITE_DEVICE_ID XPAR_UARTLITE_1_DEVICE_ID
#define INTC_DEVICE_ID XPAR_INTC_0_DEVICE_ID
#define UARTLITE_INT_IRQ_ID XPAR_INTC_0_UARTLITE_1_VEC_ID
#define GPIO_DEVICE_ID XPAR_GPIO_0_DEVICE_ID /*XPAR_GPIO2MCU_DEVICE_ID*/
/*
* The following constant controls the length of the buffers to be sent
* and received with the UartLite device.
*/
#define TEST_BUFFER_SIZE 50
/************************** Function Prototypes ******************************/
int CodeRecv(u16 DeviceId);
int UartIntConfig(XUartLite *UartLitePtr);
void SendHandler(void *CallBackRef, unsigned int EventData);
//void RecvHandler(void *CallBackRef, unsigned int EventData);
void RecvHandler(void);
void WriteFIFO(u8 data);
int ReadFIFO(void);
/************************** Variable Definitions *****************************/
XUartLite UartLite; /* The instance of the UartLite Device */
XIntc IntCtrl; /* The instance of the Interrupt Controller */
XGpio GpioOutput;
u8 ReceiveBuffer[50]={0};
u8 SendBuffer[50]={0};
/* u8 SendBuffer[50]={0x01,0x02,0x31,0x43,0x55,0xAA,0x0C,0xA9,0x08,0x01,
0x04,0x00,0x07,0x08,0x09,0x0A,0x00,0x00,0x00,0x00,
0xE4,0xA5,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
0x0E,0x0F,0x01,0x02,0x31,0x43,0x00,0x00,0x01,0x02}; */
/*
* The following counters are used to determine when the entire buffer has
* been sent and received.
*/
static volatile int TotalReceivedCount;
static volatile int TotalSentCount;
static volatile int sbuf;
/******************************************************************************/
/**
*
*
* @head FIFO头,用于串口的接收数据的写入
* @tail FIFO尾,用于串口的接收数据的读出
* @bar 写入的数据已经完成一圈
*
*
*******************************************************************************/
static volatile int head,tail,bar;
static volatile u8 hasrev;
/******************************************************************************/
/**
*
* Main function to call the UartLite interrupt example.
*
* @param None
*
* @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful
*
* @note None
*
*******************************************************************************/
int main(void)
{
sbuf=0;
if(! XGpio_Initialize(&GpioOutput, GPIO_DEVICE_ID))
return XST_FAILURE;
// if(! XGpio_SelfTest(&GpioOutput)){
// return XST_FAILURE;
// }
if (! CodeRecv(UARTLITE_DEVICE_ID))
return XST_FAILURE;
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 CodeRecv(u16 DeviceId)
{
int sum=0,k=0;
int index,temp;
int p=0;
int l = 0;
if ( XUartLite_Initialize(&UartLite, DeviceId)!= 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.
*/
if (UartIntConfig(&UartLite)!=XST_SUCCESS)
return XST_FAILURE;
/*
* Enable the interrupt of the UartLite so that interrupts will occur.
*/
XUartLite_EnableInterrupt(&UartLite);
//XUartLite_SendByte(XPAR_RS232_BASEADDR, SendBuffer[sbuf++]);
while(1){
u8 SendCMD [16]={0};
sum=0;
temp=ReadFIFO();
while(temp==-1)
temp=ReadFIFO();
if((u8)temp==0x55) {
temp=ReadFIFO();
while(temp==-1)
temp=ReadFIFO();
if((u8)temp==0xaa){
for(index=0;index<15;index++) {
temp=ReadFIFO();
while(temp==-1)
temp=ReadFIFO();
SendCMD[index]=temp;
sum +=temp;
}
sum &=0xff;
temp=ReadFIFO();
while(temp==-1)
temp=ReadFIFO();
if(sum == temp){
/*p=0;
for (k=1;k<12;k++) {
XGpio_WriteReg(XPAR_GENERIC_GPIO_BASEADDR,0,SendCMD[k+p]+((k+16)<<8));
for (sum=0;sum<2;sum++);
XGpio_WriteReg(XPAR_GENERIC_GPIO_BASEADDR,0,0);
p=(k>1)?1:0;
} */
p=0;
l = 0;
for (k=1;k<12;k++) {
XGpio_WriteReg(XPAR_GENERIC_GPIO_BASEADDR,0,SendCMD[k+p+l]+((k+16)<<8));
for (sum=0;sum<2;sum++);
XGpio_WriteReg(XPAR_GENERIC_GPIO_BASEADDR,0,0);
p=(k != 0)?1:0;
l = (k < 9 )?0:2;
}
p=0;
}
}
}
}
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)
{
//while(!XUartLite_IsReceiveEmpty(XPAR_RS232_BASEADDR))
WriteFIFO(XUartLite_RecvByte(XPAR_RS232_BASEADDR));
//XUartLite_SendByte(XPAR_RS232_BASEADDR, SendBuffer[(sbuf++)%50]);
}
/****************************************************************************/
/**
*
* 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 UartIntConfig(XUartLite *UartLitePtr)
{
int Status;
/*
* Initialize the interrupt controller driver so that it is ready to
* use.
*/
Status = XIntc_Initialize(&IntCtrl, 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(&IntCtrl, UARTLITE_INT_IRQ_ID,
(XInterruptHandler)RecvHandler,
(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(&IntCtrl, XIN_REAL_MODE);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Enable the interrupt for the UartLite device.
*/
XIntc_Enable(&IntCtrl, 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,
&IntCtrl);
/*
* Enable exceptions.
*/
Xil_ExceptionEnable();
return XST_SUCCESS;
}
/****************************************************************************/
/**
*
* 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.
*
****************************************************************************/
void WriteFIFO(u8 data)
{
ReceiveBuffer[head] =data;
if(head==49) {
bar=1;
head=0;
}
else
head++;
}
/****************************************************************************/
/**
*
* 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 ReadFIFO(void)
{
if(tail<head)
return ReceiveBuffer[tail++];
else if(tail==head)
return -1;
else if(bar) {
if(tail<49)
return ReceiveBuffer[tail++];
else {
bar=0;
tail=0;
return ReceiveBuffer[49];
}
}
else
return -1;
}
****************************************************************************************************
现成的从上位机到软核的串口软件代码,,现在需要添加往上位机回送数据的代码,,要求原样不动的回送回去就行求大神带飞。。。。
一周热门 更多>