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

📄 uart.c

📁 Jan 04, 2007 1. Add SPI support, see spi.h and spi.c 2. Add driver.h 3. Modified keyboard modu
💻 C
字号:
/** * Copyright (c) 2006-2008 iWESUN (ShenZhen) Inf. * All rights reserved.  *  * Redistribution and use in source and binary forms, with or without modification,  * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, *    this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, *    this list of conditions and the following disclaimer in the documentation *    and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products *    derived from this software without specific prior written permission.  * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY  * OF SUCH DAMAGE. * * This file is part of the AvrcX MTOS *  * Author: Winter Hu  <winter.hu@gmail.com> * Create: Dec 25, 2006 */#include "uart.h"#include "avrcx.h"#if __UART__ == 1NAKEDFIFO(rxbuf, UART_RXBUF_SIZE);NAKEDFIFO(txbuf, UART_TXBUF_SIZE);UARTCB uart = { UART_TX_EMPTY, NULL, &txbuf, &rxbuf};/** * Initialize UART for Rx and Tx * You should invoke this method to initializ UART * Currently, this method just supported 8bit data, 1bit stop and none check * */void init_uart(void){  UBRRH = HI8(UART_UBRR);  UBRRL = LO8(UART_UBRR);  UCSRB = _BV(RXCIE) | _BV(TXCIE) | _BV(RXEN) | _BV(TXEN);  UCSRC = 0x86; // Async, 8 bits data, 1 bit stop, None check}/** * Register user's hook to UART driver * * @param uart_hook, The hook to handle uart status */void uart_set_hook(uart_hook hook){  uart.hook = hook;}/** * Put a char to UART Tx queue * * @param char, the char need put to UART * @return 0 is successful, -1 is failed */int uart_putchar(char b){  int ret = 0;  into_critical();  unsigned char status = UCSRA;  if ( is_empty(uart.txbuf) && (status & _BV(UDRE))){    // Fifo is empty and UDRE, so send byte directly    UDR = b;  }else{    uart.status &= ~UART_TX_EMPTY;    ret = fifo_putbyte(uart.txbuf, b);  }  exit_critical();  return ret;}/** * Get a char from UART Rx queue * * @return the char, if equas to -1, there are some errors occur. */int uart_getchar(void){  into_critical();  int ret = fifo_getbyte(uart.rxbuf);  uart.status &= ~(UART_RECVDATA | UART_RX_FULLY);  exit_critical();  return ret;}/** * Rx complete interrupt handler */void NAKED SIG_UART_RECV(void);void SIG_UART_RECV(void){  into_critical();  prologue();    if (!(uart.status & UART_RECVDATA)){    uart.status |= UART_RECVDATA;    if (uart.hook != NULL) uart.hook(uart.status);  }  if (!(UCSRA & (_BV(FE) | _BV(DOR)))){    unsigned char b = UDR;    int err = fifo_putbyte(uart.rxbuf, b);    if (err == -1){      uart.status |= UART_RX_FULLY;      if (uart.hook != NULL) uart.hook(uart.status);    }  }    epilogue();}/** * Tx data register empty interrupt handler */void NAKED SIG_UART_TRANS(void);void SIG_UART_TRANS(void){  into_critical();  prologue();    int b = fifo_getbyte(uart.txbuf);  if (b > 0){    UDR = b;  }else{    uart.status |= UART_TX_EMPTY;    if (uart.hook != NULL) uart.hook(uart.status);  }  epilogue();}#endif

⌨️ 快捷键说明

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