⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lh79520_uart_driver.c

📁 Sharp Lh79520 Uart驱动源码.
💻 C
📖 第 1 页 / 共 3 页
字号:
                    case UART_GET_STOP_BITS:
                        /* Return the number of stop bits */
                        if ((uartregs->lcr_h & UARTLCR_STP2) != 0)
                        {
                            /* 2 stop bits */
                            status = 2;
                        }
                        else
                        {
                            status = 1;
                        }
                        break;

                    case UART_GET_FIFO_ENABLE:
                        /* Returns fifo enable status */
                        if ((uartregs->lcr_h & UARTLCR_FEN) != 0)
                        {
                            /* fifo is enabled */
                            status = 1;
                        }
                        else
                        {
                            /* fifo is disabled */
                            status = 0;
                        }
                        break;

                    case UART_GET_ASSERT_BREAK:
                        /* Returns break assert status */
                        if ((uartregs->lcr_h & UARTLCR_BRK) != 0)
                        {
                            /* break assert is enabled */
                            status = 1;
                        }
                        else
                        {
                            /* break assert is disabled */
                            status = 0;
                        }
                        break;

                    case UART_GET_LOOPBACK:
                        /* Returns Loopback mode enabled status */
                        if ((uartregs->cr & UARTCR_LBE) != 0)
                        {
                            /* Loopback mode is enabled */
                            status = 1;
                        }
                        else
                        {
                            /* Loopback mode is disabled */
                            status = 0;
                        }
                        break;

                    case SIR_GET_LOWPOWER:
                        /* Returns SIR low power mode status */
                        if ((uartregs->cr & UARTCR_SIRLP) != 0)
                        {
                            /* SIR low power mode enabled */
                            status = 1;
                        }
                        else
                        {
                            /* SIR low power mode disabled */
                            status = 0;
                        }
                        break;

                    case UART_GET_INT_ENABLE:
                        /* Return enabled interrupts */
                        status = uartregs->imsc & 
                            (UARTINT_RX | UARTINT_TX | UARTINT_RT |
                            UARTINT_FE | UARTINT_PE | UARTINT_BE | 
                            UARTINT_OE);
                        break;

                    default:
                        /* Unsupported parameter */
                        status = _ERROR;
                        break;
                }
                break;

            default:
                /* Unsupported parameter */
                status = _ERROR;
        }
    }

    return status;
}

/***********************************************************************
 *
 * Function: uart_read_polled
 *
 * Purpose: UART read function for polled mode
 *
 * Processing:
 *     If the init flag for the UART structure is FALSE, return 0 to
 *     the caller. Otherwise, loop until max_bytes equals 0 or until
 *     the receive FIFO is empty, whichever comes first. Read the data
 *     from the UART FIFO and place it user buffer. Increment the
 *     address of the user buffer. Increment bytes, and decrement
 *     max_bytes. Exit the loop based on the loop conditions and
 *     return the number of bytes read to the caller.
 *
 * Parameters:
 *     devid:     Pointer to UART config structure
 *     buffer:    Pointer to data buffer to copy to
 *     max_bytes: Number of bytes to read
 *
 * Outputs: None
 *
 * Returns: Number of bytes actually read from the FIFO
 *
 * Notes: None
 *
 **********************************************************************/
INT_32 uart_read_polled(INT_32 devid,
                        void *buffer,
                        INT_32 max_bytes)
{
    UART_REGS_T *uartregs;
    UART_CFG_T *uartcfgptr = (UART_CFG_T *) devid;
    UNS_8 *data = (UNS_8 *) buffer;
    INT_32 bytes = 0;

    if (uartcfgptr->init == TRUE)
    {
        uartregs = uartcfgptr->regptr;

        /* Loop until receive FIFO is empty or until max_bytes
           expires */
        while ((max_bytes > 0) &&
            ((uartregs->fr & UARTFR_RXFE) == 0))
        {
            /* Read data from FIFO into buffer */
            *data = (UNS_8) uartregs->dr;
            data++;

            /* Increment data count and decrement buffer size count */
            bytes++;
            max_bytes--;
        }
    }

    return bytes;
}

/***********************************************************************
 *
 * Function: uart_write_polled
 *
 * Purpose: UART write function for polled mode
 *
 * Processing:
 *     If the init flag for the UART structure is FALSE, return 0 to
 *     the caller. Otherwise, loop until n_bytes equals 0. Loop until
 *     the transmit FIFO is full insisde the n_bytes loop. Read the data
 *     from the user buffer and place it in the UART FIFO. Increment
 *     the address of the user buffer. Increment bytes, and decrement
 *     n_bytes. Exit the loop based on the loop conditions and
 *     return the number of actually bytes written to the caller.
 *
 * Parameters:
 *     devid:   Pointer to UART config structure
 *     buffer:  Pointer to data buffer to copy from
 *     n_bytes: Number of bytes to write
 *
 * Outputs: None
 *
 * Returns: Number of bytes actually written to the FIFO
 *
 * Notes: None
 *
 **********************************************************************/
INT_32 uart_write_polled(INT_32 devid,
                         void *buffer,
                         INT_32 n_bytes)
{
    UART_REGS_T *uartregs;
    UART_CFG_T *uartcfgptr = (UART_CFG_T *) devid;
    UNS_8 *data = (UNS_8 *) buffer;
    INT_32 bytes = 0;

    if (uartcfgptr->init == TRUE)
    {
        uartregs = uartcfgptr->regptr;

        /* Loop until n_bytes expires to 0 */
        while (n_bytes > 0)
        {
            if ((uartregs->fr & UARTFR_TXFF) == 0)
            {
                /* Write data from buffer into FIFO */
                uartregs->dr = *data;
                data++;

                /* Increment data count and decrement buffer size
                   count */
                bytes++;
                n_bytes--;
            }
        }
    }

    return bytes;
}

/***********************************************************************
 *
 * Function: uart_read_ring
 *
 * Purpose: UART read function for interrupt mode (using ring buffers)
 *
 * Processing:
 *     If the init flag for the UART structure is FALSE, return 0 to
 *     the caller. Otherwise, save the state of the receive interrupts
 *     and disable the receive interrupts. Loop until max_bytes equals
 *     0 or until the receive ring buffer is empty, whichever comes
 *     first. Read the data from the ring buffer  indexed by the tail
 *     pointer and place it into the user buffer. Increment the tail
 *     pointer and user buffer pointer. If the tail pointer exceeds the
 *     buffer size, set the tail pointer to 0. Increment bytes, and
 *     decrement max_bytes. Exit the loop based on the loop conditions,
 *     re-enable the receive interrupts, and return the number of bytes
 *     read to the caller.
 *
 * Parameters:
 *     devid:     Pointer to UART config structure
 *     buffer:    Pointer to data buffer to copy to
 *     max_bytes: Number of bytes to read
 *
 * Outputs: None
 *
 * Returns: Number of bytes actually read from the ring buffer
 *
 * Notes: Function can only be called after the interruption is setup
 *
 **********************************************************************/
INT_32 uart_read_ring(INT_32 devid,
                      void *buffer,
                      INT_32 max_bytes)
{
    UART_REGS_T *uartregs;
    UNS_32 tmp1;
    UART_CFG_T *uartcfgptr = (UART_CFG_T *) devid;
    UNS_8 *data = (UNS_8 *) buffer;
    INT_32 bytes = 0;

    if (uartcfgptr->init == TRUE)
    {
        uartregs = uartcfgptr->regptr;

        /* Temporarily lock out UART receive interrupts during this
           read so the UART receive interrupt won't cause problems
           with the index values */
        tmp1 = uartregs->imsc & (UARTINT_RT | UARTINT_RX);
        uartregs->imsc &= ~(UARTINT_RT | UARTINT_RX);

		/* Flush unread FIFO */
        uart_standard_receive(uartcfgptr);

        /* Loop until receive FIFO is empty or until max_bytes
           expires */
        while ((max_bytes > 0) &&
            (uartcfgptr->rb.rx_tail != uartcfgptr->rb.rx_head))
        {
            /* Read data from ring buffer into user buffer */
            *data = uartcfgptr->rb.rx[uartcfgptr->rb.rx_tail];
            data++;

            /* Update tail pointer */
            uartcfgptr->rb.rx_tail++;

            /* Make sure tail didn't overflow */
            if (uartcfgptr->rb.rx_tail >= UART_RING_BUFSIZE)
            {
                uartcfgptr->rb.rx_tail = 0;
            }

            /* Increment data count and decrement buffer size count */
            bytes++;
            max_bytes--;
        }

        /* Re-enable UART interrupts */
        uartregs->imsc |= tmp1;
    }

    return bytes;
}

/***********************************************************************
 *
 * Function: uart_write_ring
 *
 * Purpose: UART write function for interrupt mode (using ring buffers)
 *
 * Processing:
 *     If the init flag for the UART structure is FALSE, return 0 to
 *     the caller. Otherwise, disable the receive interrupts. Loop until
 *     n_bytes equals 0 or until the transmit ring buffer is full,
 *     whichever comes first. Write the data from the user buffer to
 *     the transmit ring buffer indexed by the head pointer. Increment
 *     the user buffer pointer and head pointer. If the head pointer
 *     exceeds the buffer size, set the head pointer to 0. Increment
 *     bytes, and decrement n_bytes. Exit the loop based on the loop
 *     conditions. If the number bytes written to the ring buffer was
 *     greater then 0, call the uart_standard_transmit() fucntion.
 *     Return the number of bytes read to the caller.
 *
 * Parameters:
 *     devid:   Pointer to UART config structure
 *     buffer:  Pointer to data buffer to copy from
 *     n_bytes: Number of bytes to write
 *
 * Outputs: None
 *
 * Returns: Number of bytes actually written to the ring buffer
 *
 * Notes:
 *     You cannot overflow the transmit ring buffer.
 *      Function can only be called after interruption is set up
 *
 **********************************************************************/
INT_32 uart_write_ring(INT_32 devid,
                       void *buffer,
                       INT_32 n_bytes)
{
    UART_REGS_T *uartregs;
    UART_CFG_T *uartcfgptr = (UART_CFG_T *) devid;
    UNS_8 *data = (UNS_8 *) buffer;
    INT_32 bytes = 0;

    if (uartcfgptr->init == TRUE)
    {
        uartregs = uartcfgptr->regptr;

        /* Temporarily lock out UART transmit interrupts during this
           read so the UART transmit interrupt won't cause problems
           with the index values */
        uartregs->imsc &= ~UARTINT_TX;

        /* Loop until transmit run buffer is full or until n_bytes
           expires */
        while ((n_bytes > 0) &&
            (uart_get_free_tx_count(uartcfgptr) > 0))
        {
            /* Write data from buffer into ring buffer */
            uartcfgptr->rb.tx[uartcfgptr->rb.tx_head] = *data;
            data++;

            /* Increment head pointer */
            uartcfgptr->rb.tx_head++;
            if (uartcfgptr->rb.tx_head >= UART_RING_BUFSIZE)
            {
                uartcfgptr->rb.tx_head = 0;
            }

            /* Increment data count and decrement buffer size count */
            bytes++;
            n_bytes--;
        }

        /* Now start sending the data - this will also re-enable the
           transmit interrupt */
        if (bytes > 0)
        {
            uart_standard_transmit(uartcfgptr);
        }
    }

    return bytes;
}

/***********************************************************************
 *
 * Function: uart0_isr
 *
 * Purpose: UART 0 interrupt function
 *
 * Processing:
 *     Calls the standard UART interrupt branch function with the
 *     UART0 configuration structure pointer.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
void uart0_isr(void)
{
    uart_standard_interrupt(&uart0cfg);
}

/***********************************************************************
 *
 * Function: uart1_isr
 *
 * Purpose: UART 1 interrupt function
 *
 * Processing:
 *     Calls the standard UART interrupt branch function with the
 *     UART1 configuration structure pointer.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
void uart1_isr(void)
{
    uart_standard_interrupt(&uart1cfg);
}

/***********************************************************************
 *
 * Function: uart2_isr
 *
 * Purpose: UART 2 interrupt function
 *
 * Processing:
 *     Calls the standard UART interrupt branch function with the
 *     UART2 configuration structure pointer.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
void uart2_isr(void)
{
    uart_standard_interrupt(&uart2cfg);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -