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

📄 uart.c

📁 讲述LPC2468在无操作系统条件下使用YAFFS文件系统是如何实现的以及完整的测试代码,代码部分详见lpc2468_yaffs2.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
#else
  while (!(U1LSR & ULSR_THRE))          // wait for TX buffer to empty
    continue;                           // also either WDOG() or swap()

  U1THR = (uint8_t)ch;
#endif
  return (uint8_t)ch;
}

int uart1Putch_block(int ch)
{
	while ( !uart1Space() ) {
		; // wait for space available
	}
	return uart1Putch(ch);
}

/******************************************************************************
 *
 * Function Name: uart1Space()
 *
 * Description:  
 *    This function gets the available space in the transmit queue
 *
 * Calling Sequence: 
 *    void
 *
 * Returns:
 *    available space in the transmit queue
 *
 *****************************************************************************/
uint16_t uart1Space(void)
{
#ifdef UART1_TX_INT_MODE
  int space;

  if ((space = (uart1_tx_extract_idx - uart1_tx_insert_idx)) <= 0)
    space += UART1_TX_BUFFER_SIZE;

  return (uint16_t)(space - 1);
#else
  return USHRT_MAX;
#endif
}

/******************************************************************************
 *
 * Function Name: uart1Puts()
 *
 * Description:  
 *    This function writes a NULL terminated 'string' to the UART output
 *    queue, returning a pointer to the next character to be written.
 *
 * Calling Sequence: 
 *    address of the string
 *
 * Returns:
 *    a pointer to the next character to be written
 *    (\0 if full string is written)
 *
 *****************************************************************************/
const char *uart1Puts(const char *string)
{
  register char ch;

  while ((ch = *string) && (uart1Putch(ch) >= 0))
    string++;

  return string;
}

const char *uart1Puts_block(const char *string)
{
  register char ch;

  while ((ch = *string) && (uart1Putch_block(ch) >= 0))
    string++;

  return string;
}

/******************************************************************************
 *
 * Function Name: uart1Write()
 *
 * Description:  
 *    This function writes 'count' characters from 'buffer' to the UART
 *    output queue.
 *
 * Calling Sequence: 
 *    
 *
 * Returns:
 *    0 on success, -1 if insufficient room, -2 on error
 *    NOTE: if insufficient room, no characters are written.
 *
 *****************************************************************************/
int uart1Write(const char *buffer, uint16_t count)
{
#ifdef UART1_TX_INT_MODE
  if (count > uart1Space())
    return -1;
#endif
  while (count && (uart1Putch(*buffer++) >= 0))
    count--;

  return (count ? -2 : 0);
}

/******************************************************************************
 *
 * Function Name: uart1TxEmpty()
 *
 * Description:
 *    This function returns the status of the UART transmit data
 *    registers.
 *
 * Calling Sequence: 
 *    void
 *
 * Returns:
 *    FALSE - either the tx holding or shift register is not empty
 *   !FALSE - if both the tx holding & shift registers are empty
 *
 *****************************************************************************/
int uart1TxEmpty(void)
{
  return (U1LSR & (ULSR_THRE | ULSR_TEMT)) == (ULSR_THRE | ULSR_TEMT);
}

/******************************************************************************
 *
 * Function Name: uart1TxFlush()
 *
 * Description:  
 *    This function removes all characters from the UART transmit queue
 *    (without transmitting them).
 *
 * Calling Sequence: 
 *    void
 *
 * Returns:
 *    void
 *
 *****************************************************************************/
void uart1TxFlush(void)
{
#ifdef UART1_TX_INT_MODE
  unsigned cpsr;

  U1FCR |= UFCR_TX_FIFO_RESET;          // clear the TX fifo

  // "Empty" the transmit buffer.
  cpsr = disableIRQ();                  // disable global interrupts
  U1IER &= ~UIER_THRE;                 // disable TX interrupts
  restoreIRQ(cpsr);                     // restore global interrupts
  uart1_tx_insert_idx = uart1_tx_extract_idx = 0;
#else
  U1FCR |= UFCR_TX_FIFO_RESET;          // clear the TX fifo
#endif
}

/******************************************************************************
 *
 * Function Name: uart1Getch()
 *
 * Description:  
 *    This function gets a character from the UART receive queue
 *
 * Calling Sequence: 
 *    void
 *
 * Returns:
 *    character on success, -1 if no character is available
 *
 *****************************************************************************/
int uart1Getch(void)
{
#ifdef UART1_RX_INT_MODE
  uint8_t ch;

  if (uart1_rx_insert_idx == uart1_rx_extract_idx) // check if character is available
    return -1;

  ch = uart1_rx_buffer[uart1_rx_extract_idx++]; // get character, bump pointer
  uart1_rx_extract_idx %= UART1_RX_BUFFER_SIZE; // limit the pointer
  return ch;
#else
  if (U1LSR & ULSR_RDR)                 // check if character is available
    return U1RBR;                       // return character

  return -1;
#endif
}

#endif

#if UART3_SUPPORT

/******************************************************************************
 *
 * Function Name: uart0Init()
 *
 * Description:  
 *    This function initializes the UART for async mode
 *
 * Calling Sequence: 
 *    baudrate divisor - use UART_BAUD macro
 *    mode - see typical modes (uart.h)
 *    fmode - see typical fmodes (uart.h)
 *
 * Returns:
 *    void
 *
 * NOTE: uart0Init(UART_BAUD(9600), UART_8N1, UART_FIFO_8);
 *
 *****************************************************************************/
void uart3Init(uint16_t baud, uint8_t mode, uint8_t fmode)
{
  // set port pins for UART0
  // mthomas PINSEL0 = (PINSEL0 & ~U0_PINMASK) | U0_PINSEL;
  // set port pins for UART0
  // PINSEL0 = (PINSEL0 & ~U0_PINMASK) | U0_PINSEL;
  U3_TX_PINSEL_REG = ( U3_TX_PINSEL_REG & ~U3_TX_PINMASK ) | U3_TX_PINSEL;
  U3_RX_PINSEL_REG = ( U3_RX_PINSEL_REG & ~U3_RX_PINMASK ) | U3_RX_PINSEL;

  U3IER = 0x00;                         // disable all interrupts
  U3IIR;                                // clear interrupt ID
  U3RBR;                                // clear receive register
  U3LSR;                                // clear line status register

  // set the baudrate
  U3LCR = ULCR_DLAB_ENABLE;             // select divisor latches 
  U3DLL = (uint8_t)baud;                // set for baud low byte
  U3DLM = (uint8_t)(baud >> 8);         // set for baud high byte

  // set the number of characters and other
  // user specified operating parameters
  U3LCR = (mode & ~ULCR_DLAB_ENABLE);
  U3FCR = fmode;
  U3TER = (1<<7);

#if defined(UART3_TX_INT_MODE) || defined(UART3_RX_INT_MODE)
  // initialize the interrupt vector
  VICIntSelect    &= ~VIC_CHAN_TO_MASK(VIC_CHAN_NUM_UART3);
  VICIntEnClr      = VIC_CHAN_TO_MASK(VIC_CHAN_NUM_UART3); 
  VICVectAddr9     = (uint32_t)uart3ISR;
  VICVectPriority9 = 0xF;
  VICIntEnable     = VIC_CHAN_TO_MASK(VIC_CHAN_NUM_UART3);
#if 0
  VICIntSelect &= ~VIC_BIT(VIC_UART3);  // UART0 selected as IRQ
  VICIntEnable = VIC_BIT(VIC_UART3);    // UART0 interrupt enabled
  VICVectCntl0 = VIC_ENABLE | VIC_UART3;
  VICVectAddr0 = (uint32_t)uart3ISR;    // address of the ISR
#endif
#ifdef UART3_TX_INT_MODE
  // initialize the transmit data queue
  uart3_tx_extract_idx = uart3_tx_insert_idx = 0;
  uart3_tx_running = 0;
#endif

#ifdef UART3_RX_INT_MODE
  // initialize the receive data queue
  uart3_rx_extract_idx = uart3_rx_insert_idx = 0;

  // enable receiver interrupts
  U3IER = UIER_RBR;
#endif
#endif
}

/******************************************************************************
 *
 * Function Name: uart0Putch()
 *
 * Description:  
 *    This function puts a character into the UART output queue for
 *    transmission.
 *
 * Calling Sequence: 
 *    character to be transmitted
 *
 * Returns:
 *    ch on success, -1 on error (queue full)
 *
 *****************************************************************************/
int uart3Putch(int ch)
{
#ifdef UART3_TX_INT_MODE
  uint16_t temp;
  unsigned cpsr;

  temp = (uart3_tx_insert_idx + 1) % UART3_TX_BUFFER_SIZE;

  if (temp == uart3_tx_extract_idx)
    return -1;                          // no room

  cpsr = disableIRQ();                  // disable global interrupts
  U3IER &= ~UIER_THRE;                  // disable TX interrupts
  restoreIRQ(cpsr);                     // restore global interrupts

  // check if in process of sending data
  if (uart3_tx_running)
    {
    // add to queue
    uart3_tx_buffer[uart3_tx_insert_idx] = (uint8_t)ch;
    uart3_tx_insert_idx = temp;
    }
  else
    {
    // set running flag and write to output register
    uart3_tx_running = 1;
    U3THR = (uint8_t)ch;
    }

  cpsr = disableIRQ();                  // disable global interrupts
  U3IER |= UIER_THRE;                   // enable TX interrupts
  restoreIRQ(cpsr);                     // restore global interrupts
#else
  while (!(U3LSR & ULSR_THRE))          // wait for TX buffer to empty
    continue;                           // also either WDOG() or swap()

  U3THR = (uint8_t)ch;
#endif
  return (uint8_t)ch;
}

/******************************************************************************
 *
 * Function Name: uart0Space()
 *
 * Description:  
 *    This function gets the available space in the transmit queue
 *
 * Calling Sequence: 
 *    void
 *
 * Returns:
 *    available space in the transmit queue
 *
 *****************************************************************************/
uint16_t uart3Space(void)
{
#ifdef UART3_TX_INT_MODE
  int space;

  if ((space = (uart3_tx_extract_idx - uart3_tx_insert_idx)) <= 0)
    space += UART3_TX_BUFFER_SIZE;

  return (uint16_t)(space - 1);
#else
  return USHRT_MAX;
#endif
}

/******************************************************************************
 *
 * Function Name: uart0Puts()
 *
 * Description:  
 *    This function writes a NULL terminated 'string' to the UART output
 *    queue, returning a pointer to the next character to be written.
 *
 * Calling Sequence: 
 *    address of the string
 *
 * Returns:
 *    a pointer to the next character to be written
 *    (\0 if full string is written)
 *
 *****************************************************************************/
const char *uart3Puts(const char *string)
{
  register char ch;

  while ((ch = *string) && (uart3Putch(ch) >= 0))
    string++;

  return string;
}

/******************************************************************************
 *
 * Function Name: uart0Write()
 *
 * Description:  
 *    This function writes 'count' characters from 'buffer' to the UART
 *    output queue.
 *
 * Calling Sequence: 
 *    
 *
 * Returns:
 *    0 on success, -1 if insufficient room, -2 on error
 *    NOTE: if insufficient room, no characters are written.
 *
 *****************************************************************************/
int uart3Write(const char *buffer, uint16_t count)
{
#ifdef UART3_TX_INT_MODE
  if (count > uart3Space())
    return -1;
#endif
  while (count && (uart3Putch(*buffer++) >= 0))
    count--;

  return (count ? -2 : 0);
}

/******************************************************************************
 *
 * Function Name: uart0TxEmpty()
 *
 * Description:
 *    This function returns the status of the UART transmit data
 *    registers.
 *
 * Calling Sequence: 
 *    void
 *
 * Returns:
 *    FALSE - either the tx holding or shift register is not empty
 *   !FALSE - if both the tx holding & shift registers are empty
 *
 *****************************************************************************/
int uart3TxEmpty(void)
{
  return (U3LSR & (ULSR_THRE | ULSR_TEMT)) == (ULSR_THRE | ULSR_TEMT);
}

/******************************************************************************
 *
 * Function Name: uart0TxFlush()
 *
 * Description:  
 *    This function removes all characters from the UART transmit queue
 *    (without transmitting them).
 *
 * Calling Sequence: 
 *    void
 *
 * Returns:
 *    void
 *
 *****************************************************************************/
void uart3TxFlush(void)
{
#ifdef UART3_TX_INT_MODE
  unsigned cpsr;

  U3FCR |= UFCR_TX_FIFO_RESET;          // clear the TX fifo

  // "Empty" the transmit buffer.
  cpsr = disableIRQ();                  // disable global interrupts
  U3IER &= ~UIER_THRE;                 // disable TX interrupts
  restoreIRQ(cpsr);                     // restore global interrupts
  uart3_tx_insert_idx = uart3_tx_extract_idx = 0;
#else
  U3FCR |= UFCR_TX_FIFO_RESET;          // clear the TX fifo
#endif
}

/******************************************************************************
 *
 * Function Name: uart0Getch()
 *
 * Description:  
 *    This function gets a character from the UART receive queue
 *
 * Calling Sequence: 
 *    void
 *
 * Returns:
 *    character on success, -1 if no character is available
 *
 *****************************************************************************/
int uart3Getch(void)
{
#ifdef UART3_RX_INT_MODE
  uint8_t ch;

  if (uart3_rx_insert_idx == uart3_rx_extract_idx) // check if character is available
    return -1;

  ch = uart3_rx_buffer[uart3_rx_extract_idx++]; // get character, bump pointer
  uart3_rx_extract_idx %= UART3_RX_BUFFER_SIZE; // limit the pointer
  return ch;
#else
  if (U3LSR & ULSR_RDR)                 // check if character is available
    return U3RBR;                       // return character

  return -1;
#endif
}

#endif

⌨️ 快捷键说明

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