Esempio n. 1
0
File: serial.c Progetto: wugsh/wgs
/*
 * See the serial2.h header file.
 */
xComPortHandle xSerialPortInitMinimal( uint32_t ulWantedBaud, UBaseType_t uxQueueLength )
{
BaseType_t xStatus;
XUartPs_Config *pxConfig;

	/* Create the queue used to hold received characters.  NOTE THE COMMENTS AT
	THE TOP OF THIS FILE REGARDING THE USE OF QUEUES FOR THIS PURPSOE. */
	xRxQueue = xQueueCreate( uxQueueLength, sizeof( char ) );
	configASSERT( xRxQueue );

	/* Create the semaphore used to signal the end of a transmission, then take
	the semaphore so it is in the correct state the first time
	xSerialSendString() is called.  A block time of zero is used when taking
	the semaphore as it is guaranteed to be available (it was just created). */
	xTxCompleteSemaphore = xSemaphoreCreateBinary();
	configASSERT( xTxCompleteSemaphore );
	xSemaphoreTake( xTxCompleteSemaphore, 0 );

	/* Look up the UART configuration then initialise the dirver. */
	pxConfig = XUartPs_LookupConfig( XPAR_XUARTPS_0_DEVICE_ID );

	/* Initialise the driver. */
	xStatus = XUartPs_CfgInitialize( &xUARTInstance, pxConfig, XPAR_PS7_UART_1_BASEADDR );
	configASSERT( xStatus == XST_SUCCESS );
	( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */

	/* Misc. parameter configuration. */
	XUartPs_SetBaudRate( &xUARTInstance, ulWantedBaud );
	XUartPs_SetOperMode( &xUARTInstance, XUARTPS_OPER_MODE_NORMAL );

	/* Install the interrupt service routine that is defined within this
	file. */
	xStatus = XScuGic_Connect( &xInterruptController, XPAR_XUARTPS_1_INTR,  (Xil_ExceptionHandler) prvUART_Handler, (void *) &xUARTInstance );
	configASSERT( xStatus == XST_SUCCESS );
	( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */

	/* Ensure interrupts start clear. */
	XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, XUARTPS_IXR_MASK );

	/* Enable the UART interrupt within the GIC. */
	XScuGic_Enable( &xInterruptController, XPAR_XUARTPS_1_INTR );

	/* Enable the interrupts of interest in the UART. */
	XUartPs_SetInterruptMask( &xUARTInstance, XUARTPS_IXR_RXFULL | XUARTPS_IXR_RXOVR | XUARTPS_IXR_TOUT | XUARTPS_IXR_TXEMPTY );

	/* Set the receive timeout. */
	XUartPs_SetRecvTimeout( &xUARTInstance, 8 );

	return ( xComPortHandle ) 0;
}
/**
*
* This function does a minimal test on the UartPS device and driver as a
* design example. The purpose of this function is to illustrate
* how to use the XUartPs driver.
*
* This function sends data and expects to receive the same data through the
* device using the local loopback mode.
*
* This function uses interrupt mode of the device.
*
* @param	IntcInstPtr is a pointer to the instance of the Scu Gic driver.
* @param	UartInstPtr is a pointer to the instance of the UART driver
*		which is going to be connected to the interrupt controller.
* @param	DeviceId is the device Id of the UART device and is typically
*		XPAR_<UARTPS_instance>_DEVICE_ID value from xparameters.h.
* @param	UartIntrId is the interrupt Id and is typically
*		XPAR_<UARTPS_instance>_INTR 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 UartPsIntrExample(XScuGic *IntcInstPtr, XUartPs *UartInstPtr,
                      u16 DeviceId, u16 UartIntrId)
{
    int Status;
    XUartPs_Config *Config;
    int Index;
    u32 IntrMask;
    int BadByteCount = 0;

    /*
     * Initialize the UART driver so that it's ready to use
     * Look up the configuration in the config table, then initialize it.
     */
    Config = XUartPs_LookupConfig(DeviceId);
    if (NULL == Config) {
        return XST_FAILURE;
    }

    Status = XUartPs_CfgInitialize(UartInstPtr, Config, Config->BaseAddress);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    /*
     * Check hardware build
     */
    Status = XUartPs_SelfTest(UartInstPtr);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    /*
     * Connect the UART to the interrupt subsystem such that interrupts
     * can occur. This function is application specific.
     */
    Status = SetupInterruptSystem(IntcInstPtr, UartInstPtr, UartIntrId);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    /*
     * 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;
    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);


    /*
     * Initialize the send buffer bytes with a pattern 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 % 26) + 'A';

        RecvBuffer[Index] = 0;
    }

    /*
     * Start receiving data before sending it since there is a loopback,
     * ignoring the number of bytes received as the return value since we
     * know it will be zero
     */
    XUartPs_Recv(UartInstPtr, RecvBuffer, TEST_BUFFER_SIZE);

    /*
     * Send the buffer using the UART and ignore the number of bytes sent
     * as the return value since we are using it in interrupt mode.
     */
    XUartPs_Send(UartInstPtr, 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 (1) {
        if ((TotalSentCount == TEST_BUFFER_SIZE) &&
                (TotalReceivedCount == TEST_BUFFER_SIZE)) {
            break;
        }
    }

    /*
     * Verify the entire receive buffer was successfully received
     */
    for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
        if (RecvBuffer[Index] != SendBuffer[Index]) {
            BadByteCount++;
        }
    }



    /*
     * Set the UART in Normal Mode
     */
    XUartPs_SetOperMode(UartInstPtr, XUARTPS_OPER_MODE_NORMAL);


    /*
     * If any bytes were not correct, return an error
     */
    if (BadByteCount != 0) {
        return XST_FAILURE;
    }

    return XST_SUCCESS;
}
Esempio n. 3
0
int setupUartControl(){
	int Status;
	u32 IntrMask;

	Config = XUartPs_LookupConfig(UART_DEVICE_ID);
	if (NULL == Config) {
		return XST_FAILURE;
	}

	Status = XUartPs_CfgInitialize(&Uart_Ps, Config, Config->BaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	XUartPs_SetBaudRate(&Uart_Ps, 115200);

	/*
	 * Check hardware build.
	 */
	Status = XUartPs_SelfTest(&Uart_Ps);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Connect the UART to the interrupt subsystem such that interrupts
	 * can occur. This function is application specific.
	 */
	Status = SetupInterruptSystem(&InterruptController, &Uart_Ps, UART_INT_IRQ_ID);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * 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(&Uart_Ps, (XUartPs_Handler)Handler, &Uart_Ps);

	/*
	 * Enable the interrupt of the UART so interrupts will occur
	 */
	IntrMask =
			XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY | XUARTPS_IXR_FRAMING |
			XUARTPS_IXR_OVER | XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXFULL |
	   		XUARTPS_IXR_RXOVR;
	XUartPs_SetInterruptMask(&Uart_Ps, IntrMask);

	/*
	 * 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(&Uart_Ps, 255);

	return 0;

}
Esempio n. 4
0
int main()
{
	char buff[4];
    init_platform();




    int Status;

	XUartPs_Config *Config;
	int Index;
	u32 IntrMask = 0;
	int BadByteCount = 0;

    if (XGetPlatform_Info() == XPLAT_ZYNQ_ULTRA_MP) {
    	#ifdef XPAR_XUARTPS_1_DEVICE_ID
    		DeviceId = XPAR_XUARTPS_1_DEVICE_ID;
    	#endif
    }

    	/*
    	 * Initialize the UART driver so that it's ready to use
    	 * Look up the configuration in the config table, then initialize it.
    	 */
    	Config = XUartPs_LookupConfig(UART_DEVICE_ID);
    	if (NULL == Config) {
    		return XST_FAILURE;
    	}


    	Status = XUartPs_CfgInitialize(&uart, Config, Config->BaseAddress);
    	if (Status != XST_SUCCESS) {
    		return XST_FAILURE;
    	}

    	/* Check hardware build */
    	Status = XUartPs_SelfTest(&uart);
    	if (Status != XST_SUCCESS) {
    		return XST_FAILURE;
    	}

    	/*
    	 * Connect the UART to the interrupt subsystem such that interrupts
    	 * can occur. This function is application specific.
    	 */
    	Status = SetupInterruptSystem(&intc, &uart, UART_INT_IRQ_ID);
    	if (Status != XST_SUCCESS) {
    		return XST_FAILURE;
    	}

    	//force receive interrupt for every byte (char)
    	XUartPs_SetFifoThreshold(&uart, 1);

    	/*
    	 * 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(&uart, (XUartPs_Handler)Handler, &uart);

    	/*
    	 * Enable the interrupt of the UART so interrupts will occur
    	 */
    	IntrMask =
    		XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY | XUARTPS_IXR_FRAMING |
    		XUARTPS_IXR_OVER | XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXFULL |
    		XUARTPS_IXR_RXOVR;

    	if (uart.Platform == XPLAT_ZYNQ_ULTRA_MP) {
    		IntrMask |= XUARTPS_IXR_RBRK;
    	}

    	XUartPs_SetInterruptMask(&uart, IntrMask);

    	//XUartPs_SetOperMode(UartInstPtr, XUARTPS_OPER_MODE_LOCAL_LOOP);
    	/* Set the UART in Normal Mode */
    	XUartPs_SetOperMode(&uart, XUARTPS_OPER_MODE_NORMAL);

    	/*
    	 * 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(&uart, 8);



    	/* Run the UartPs Interrupt example, specify the the Device ID */
    	 //currently sets up some of uart.Need to pull out what is needed
    	Status = UartPsIntrExample(&intc, &uart,UART_DEVICE_ID, UART_INT_IRQ_ID);

    	/* if (Status != XST_SUCCESS) {
    		xil_printf("UART Interrupt Example Test Failed\r\n");
    		return XST_FAILURE;
    	} */

    	xil_printf("Successfully ran UART Interrupt Example Test\r\n");
    	xil_printf("count = %i\r\n", count);
    	return XST_SUCCESS;



    cleanup_platform();
    return 0;
}
/***************************************************************************
* Looks up the current config and initializes psGenericInterruptController(GIC)
***************************************************************************/
void init_GIC()
{

    int Status;
    XScuGic_Config *Config;

    /* Initialize GIC */
    Config = XScuGic_LookupConfig(INTCONTROLLER_DEVICE_ID);
    Status = XScuGic_CfgInitialize(&InterruptInstancePtr, Config, Config->CpuBaseAddress);
    if(Status != XST_FAILURE) {
        xil_printf("InterruptConroller initialized!\n \r");
    }


    /* Connect to the handler */
    Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,(Xil_ExceptionHandler)XScuGic_InterruptHandler,&InterruptInstancePtr);

    Xil_ExceptionEnable();

    /* ADC setup */

    Status = XScuGic_Connect(&InterruptInstancePtr,ADC_WIZ_INT_ID,(Xil_ExceptionHandler) ADC_Intr_Handler, (void *) &InterruptInstancePtr);
    XScuGic_Enable(&InterruptInstancePtr,ADC_WIZ_INT_ID);
    XScuGic_SetPriorityTriggerType(&InterruptInstancePtr,ADC_WIZ_INT_ID,0xA0,0x3);
    if(Status != XST_FAILURE) {
        xil_printf("ADC interrupts OK!!\n \r");
    }

    /* UART0 setup */


    XUartPs_SetInterruptMask(&Uart0InstancePtr, UART0_INT_MASK);
    XUartPs_SetHandler(&Uart0InstancePtr, (XUartPs_Handler) UART_Intr_Handler,(void *) UART0_DEVICE_ID);
    //XUartPs_InterruptHandler(&Uart1InstancePtr);
    if(Status != XST_FAILURE) {
        xil_printf("UART0 interrupts OK!!\n \r");
    }


    /* UART1 setup */

    *Intrpt_en_reg1 = UART1_INT_MASK;
    *Intrpt_dis_reg1 = 0xFFFFFD3D;

    xil_printf("Interruptmask for UART1 = %x \n \r",XUartPs_GetInterruptMask(&Uart1InstancePtr));

    /*XUartPs_SetInterruptMask(&Uart1InstancePtr, UART1_INT_MASK);
    xil_printf("Interruptmask for UART1 = %x \n \r",XUartPs_GetInterruptMask(&Uart1InstancePtr));
    XUartPs_SetHandler(&Uart1InstancePtr,(XUartPs_Handler) UART_Intr_Handler,(void *) UART1_DEVICE_ID);*/
    Status = XScuGic_Connect(&InterruptInstancePtr,UART1_INT_ID,(Xil_ExceptionHandler) UART_Intr_Handler, (void *) &InterruptInstancePtr);
    XScuGic_Enable(&InterruptInstancePtr,UART1_INT_ID);
    XScuGic_SetPriorityTriggerType(&InterruptInstancePtr,UART1_INT_ID,0xA0,0x3);
    if(Status != XST_FAILURE) {
        xil_printf("UART1 interrupts OK!!\n \r");
    }


    /* Buttons setup */


    Status = XScuGic_Connect(&InterruptInstancePtr,GPIO_BTNS_INT_ID,(Xil_ExceptionHandler) BTNS_Intr_Handler, (void *) &InterruptInstancePtr);
    XScuGic_Enable(&InterruptInstancePtr,GPIO_BTNS_INT_ID);
    XScuGic_SetPriorityTriggerType(&InterruptInstancePtr,GPIO_BTNS_INT_ID,0xA0,0x3);
    if(Status != XST_FAILURE) {
        xil_printf("BUTTONS interrupts OK!!\n \r");
    }
}