• Main Page
  • Modules
  • Index
  • File List
  • Globals

hal/nrf24lu1p/hal_uart.c

Go to the documentation of this file.
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 #include "nrf24lu1p.h"
00019 #include <stdint.h>
00020 #include "hal_uart.h"
00021 
00022 #ifndef UART_NBUF
00023 #define UART_NBUF   8
00024 #endif
00025 
00026 #define BAUD_57K6   1015  // = Round(1024 - (2*16e6)/(64*57600))
00027 #define BAUD_38K4   1011  // = Round(1024 - (2*16e6)/(64*38400))
00028 #define BAUD_19K2    998  // = Round(1024 - (2*16e6)/(64*19200))
00029 #define BAUD_9K6     972  // = Round(1024 - (2*16e6)/(64*9600))
00030 
00031 static uint8_t uart_tx_wp, uart_tx_rp, uart_tx_cnt;
00032 static uint8_t idata uart_tx[UART_NBUF];
00033 
00034 static uint8_t uart_rx_wp, uart_rx_rp, uart_rx_cnt;
00035 static uint8_t idata uart_rx[UART_NBUF];
00036 
00037 UART0_ISR()
00038 {
00039   if (RI0 == 1)
00040   {
00041     RI0 = 0;
00042     if (uart_rx_cnt < UART_NBUF)
00043     {
00044       uart_rx[uart_rx_wp] = S0BUF;
00045       uart_rx_wp = (uart_rx_wp + 1) % UART_NBUF;
00046       uart_rx_cnt++;
00047     }
00048   }
00049   if (TI0 == 1)
00050   {
00051     TI0 = 0;
00052     if (uart_tx_cnt > 1)
00053     {
00054       S0BUF = uart_tx[uart_tx_rp];
00055       uart_tx_rp = (uart_tx_rp + 1) % UART_NBUF;
00056     }
00057     uart_tx_cnt--;
00058   }
00059 }
00060 
00061 void hal_uart_init(hal_uart_baudrate_t baud)
00062 {
00063   uint16_t temp;
00064 
00065   ES0 = 0;                      // Disable UART0 interrupt while initializing
00066   uart_tx_wp = uart_tx_rp = 0;
00067   uart_tx_cnt = 0;
00068   uart_rx_wp = uart_rx_rp = 0;
00069   uart_rx_cnt = 0;
00070   REN0 = 1;                     // Enable receiver
00071   SM0 = 0;                      // Mode 1..
00072   SM1 = 1;                      // ..8 bit variable baud rate
00073   PCON |= 0x80;                 // SMOD = 1
00074   WDCON |= 0x80;                // Select internal baud rate generator
00075   switch(baud)
00076   {
00077     case UART_BAUD_57K6:
00078       temp = BAUD_57K6;
00079       break;
00080     case UART_BAUD_38K4:
00081       temp = BAUD_38K4;
00082       break;
00083     case UART_BAUD_9K6:
00084       temp = BAUD_9K6;
00085       break;
00086     case UART_BAUD_19K2:
00087     default:
00088       temp = BAUD_19K2;
00089       break;
00090   }
00091   S0RELL = (uint8_t)temp;
00092   S0RELH = (uint8_t)(temp >> 8);
00093   P0ALT |= 0x06;                // Select alternate functions on P01 and P02
00094   P0EXP &= 0xf0;                // Select RxD on P01 and TxD on P02
00095   P0DIR |= 0x02;                // P01 (RxD) is input
00096   TI0 = 0;
00097   ES0 = 1;                      // Enable UART0 interrupt
00098 }
00099 
00100 void hal_uart_putchar(uint8_t ch)
00101 {
00102   // Wait until there is space in the TX buffer:
00103   while(uart_tx_cnt > UART_NBUF)
00104     ;
00105   ES0 = 0;
00106   if (uart_tx_cnt == 0)
00107   {
00108     S0BUF = ch;                 // Write first char directly to the UART SFR
00109   }
00110   else
00111   {
00112     uart_tx[uart_tx_wp] = ch;
00113     uart_tx_wp = (uart_tx_wp + 1) % UART_NBUF;
00114   }
00115   uart_tx_cnt++;
00116   ES0 = 1;
00117 }
00118 
00119 uint8_t hal_uart_chars_available(void)
00120 {
00121   return uart_rx_cnt;
00122 }
00123 
00124 uint8_t hal_uart_getchar(void)
00125 {
00126   uint8_t ch;
00127 
00128   // Wait until a character is available:
00129   while(uart_rx_cnt == 0)
00130     ;
00131   ES0 = 0;
00132   ch = uart_rx[uart_rx_rp];
00133   uart_rx_rp = (uart_rx_rp + 1) % UART_NBUF;
00134   uart_rx_cnt--;
00135   ES0 = 1;
00136   return ch;
00137 }

Generated on Fri Apr 20 2012 14:11:45 for nRFGo SDK by  doxygen 1.7.2