Example #1
0
File: main.c Project: gstroe/Arm
/**
 * USART interrupt handler
 */
void USART2_Handler(void)
{
	Usart *pUs = BASE_USART;
	uint32_t status;
	uint16_t serialState;
	uint32_t count;

	status  = USART_GetStatus(pUs);
	status &= USART_GetItMask(pUs);

	/* If USB device is not configured, do nothing */
	if (!isCdcSerialON) {
		USART_DisableIt(pUs, 0xFFFFFFFF);
		return;
	}

	if (status & US_CSR_TIMEOUT) {
		/*Clear TIMEOUT Flag and Start Time-out After Next Character Received*/
		USART_AcknowledgeRxTimeOut(BASE_USART, 0);
		/* Flush the DMA FIFO */
		XDMAC_SoftwareFlushReq(dmad.pXdmacs, usartDmaRxChannel);
		/* Transfer the last pack through USB */
		count = dmad.pXdmacs->XDMAC_CHID[usartDmaRxChannel].XDMAC_CUBC;
		SCB_InvalidateDCache_by_Addr((uint32_t *)usartBuffers, DATAPACKETSIZE - count);

		while (CDCDSerialDriver_Write(usartBuffers, DATAPACKETSIZE - count, 0, 0)
			   != USBD_STATUS_SUCCESS);

		/*Reset DMA transfer*/
		XDMAD_StopTransfer(&dmad, usartDmaRxChannel);
		_UsartDmaRx();
	} else {
		/* Errors */
		serialState = CDCDSerialDriver_GetSerialState();

		/* Overrun */
		if ((status & US_CSR_OVRE) != 0) {
			TRACE_WARNING("USART_IrqHandler: Overrun\n\r");
			serialState |= CDCSerialState_OVERRUN;
		}

		/* Framing error */
		if ((status & US_CSR_FRAME) != 0) {
			TRACE_WARNING("USART_IrqHandler: Framing error\n\r");
			serialState |= CDCSerialState_FRAMING;
		}

		CDCDSerialDriver_SetSerialState(serialState);
	}
}
Example #2
0
//------------------------------------------------------------------------------
/// Handles interrupts coming from USART #0.
//------------------------------------------------------------------------------
static void ISR_Usart0()
{
    unsigned int status = AT91C_BASE_US0->US_CSR;
    unsigned short serialState;

    // If USB device is not configured, do nothing
    if (USBD_GetState() != USBD_STATE_CONFIGURED) {

        AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;
        return;
    }

    // Buffer has been read successfully
    if ((status & AT91C_US_ENDRX) != 0) {

        // Disable timer
        AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;

        // Send buffer through the USB
        while (CDCDSerialDriver_Write(usartBuffers[usartCurrentBuffer],
                                      DATABUFFERSIZE, 0, 0) != USBD_STATUS_SUCCESS);

        // Restart read on buffer
        USART_ReadBuffer(AT91C_BASE_US0,
                         usartBuffers[usartCurrentBuffer],
                         DATABUFFERSIZE);
        usartCurrentBuffer = 1 - usartCurrentBuffer;

        // Restart timer
        AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
    }

    // Buffer has been sent
    if ((status & AT91C_US_TXBUFE) != 0) {

        // Restart USB read
        CDCDSerialDriver_Read(usbBuffer,
                              DATABUFFERSIZE,
                              (TransferCallback) UsbDataReceived,
                              0);
        AT91C_BASE_US0->US_IDR = AT91C_US_TXBUFE;
    }

    // Errors
    serialState = CDCDSerialDriver_GetSerialState();

    // Overrun
    if ((status & AT91C_US_OVER) != 0) {

        TRACE_WARNING( "ISR_Usart0: Overrun\n\r");
        serialState |= CDCDSerialDriver_STATE_OVERRUN;
    }

    // Framing error
    if ((status & AT91C_US_FRAME) != 0) {

        TRACE_WARNING( "ISR_Usart0: Framing error\n\r");
        serialState |= CDCDSerialDriver_STATE_FRAMING;
    }

    CDCDSerialDriver_SetSerialState(serialState);
}