00001 /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved. 00002 * 00003 * The information contained herein is confidential property of Nordic 00004 * Semiconductor ASA.Terms and conditions of usage are described in detail 00005 * in NORDIC SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. 00006 * 00007 * Licensees are granted free, non-transferable use of the information. NO 00008 * WARRENTY of ANY KIND is provided. This heading must NOT be removed from 00009 * the file. 00010 * 00011 * $LastChangedRevision: 133 $ 00012 */ 00013 00018 //lint -e788 00019 //lint -e714 00020 00021 #include "nrf24le1.h" 00022 #include <stdint.h> 00023 #include <stdbool.h> 00024 #include "hal_uart.h" 00025 00026 #ifndef UART_NBUF 00027 #define UART_NBUF 8 00028 #endif 00029 00030 #define BAUD_57K6 1015 // = Round(1024 - (2*16e6)/(64*57600)) 00031 #define BAUD_38K4 1011 // = Round(1024 - (2*16e6)/(64*38400)) 00032 #define BAUD_19K2 998 // = Round(1024 - (2*16e6)/(64*19200)) 00033 #define BAUD_9K6 972 // = Round(1024 - (2*16e6)/(64*9600)) 00034 00035 static uint8_t uart_tx_wp, uart_tx_rp, uart_tx_cnt; 00036 static uint8_t idata uart_tx[UART_NBUF]; 00037 00038 static uint8_t uart_rx_wp, uart_rx_rp, uart_rx_cnt; 00039 static uint8_t idata uart_rx[UART_NBUF]; 00040 00041 UART0_ISR() 00042 { 00043 if (RI0 == 1) 00044 { 00045 RI0 = 0; 00046 if (uart_rx_cnt < UART_NBUF) 00047 { 00048 uart_rx[uart_rx_wp] = S0BUF; 00049 uart_rx_wp = (uart_rx_wp + 1) % UART_NBUF; 00050 uart_rx_cnt++; 00051 } 00052 } 00053 if (TI0 == 1) 00054 { 00055 TI0 = 0; 00056 if (uart_tx_cnt > 1) 00057 { 00058 S0BUF = uart_tx[uart_tx_rp]; 00059 uart_tx_rp = (uart_tx_rp + 1) % UART_NBUF; 00060 } 00061 uart_tx_cnt--; 00062 } 00063 } 00064 00065 void hal_uart_init(hal_uart_baudrate_t baud) 00066 { 00067 uint16_t temp; 00068 00069 ES0 = 0; // Disable UART0 interrupt while initializing 00070 uart_tx_wp = uart_tx_rp = 0; 00071 uart_tx_cnt = 0; 00072 uart_rx_wp = uart_rx_rp = 0; 00073 uart_rx_cnt = 0; 00074 REN0 = 1; // Enable receiver 00075 SM0 = 0; // Mode 1.. 00076 SM1 = 1; // ..8 bit variable baud rate 00077 PCON |= 0x80; // SMOD = 1 00078 ADCON |= 0x80; // Select internal baud rate generator 00079 switch(baud) 00080 { 00081 case UART_BAUD_57K6: 00082 temp = BAUD_57K6; 00083 break; 00084 case UART_BAUD_38K4: 00085 temp = BAUD_38K4; 00086 break; 00087 case UART_BAUD_9K6: 00088 temp = BAUD_9K6; 00089 break; 00090 case UART_BAUD_19K2: 00091 default: 00092 temp = BAUD_19K2; 00093 break; 00094 } 00095 S0RELL = (uint8_t)temp; 00096 S0RELH = (uint8_t)(temp >> 8); 00097 00098 TI0 = 0; 00099 ES0 = 1; // Enable UART0 interrupt 00100 } 00101 00102 void hal_uart_putchar(uint8_t ch) 00103 { 00104 // Wait until there is space in the TX buffer: 00105 while(uart_tx_cnt > UART_NBUF) 00106 ; 00107 ES0 = 0; 00108 if (uart_tx_cnt == 0) 00109 { 00110 S0BUF = ch; // Write first char directly to the UART SFR 00111 } 00112 else 00113 { 00114 uart_tx[uart_tx_wp] = ch; 00115 uart_tx_wp = (uart_tx_wp + 1) % UART_NBUF; 00116 } 00117 uart_tx_cnt++; 00118 ES0 = 1; 00119 } 00120 00121 00122 uint8_t hal_uart_chars_available(void) 00123 { 00124 return uart_rx_cnt; 00125 } 00126 00127 bool hal_uart_tx_complete() 00128 { 00129 if(uart_tx_cnt == 0) 00130 { 00131 return true; 00132 } 00133 else 00134 { 00135 return false; 00136 } 00137 } 00138 uint8_t hal_uart_getchar(void) 00139 { 00140 uint8_t ch; 00141 00142 // Wait until a character is available: 00143 00144 while(uart_rx_cnt == 0) 00145 {} 00146 ES0 = 0; 00147 ch = uart_rx[uart_rx_rp]; 00148 uart_rx_rp = (uart_rx_rp + 1) % UART_NBUF; 00149 uart_rx_cnt--; 00150 ES0 = 1; 00151 return ch; 00152 } 00153