?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /*-------------------------------------------------------------------------
  2.    _ser.c - this file contains a simple interrupt driven serial driver with
  3.             buffer (no check for overflow!!!).
  4.  
  5.    Copyright (C) 1999, Sandeep Dutta <sandeep.dutta AT ieee.org>
  6.  
  7.    This library is free software; you can redistribute it and/or modify it
  8.    under the terms of the GNU General Public License as published by the
  9.    Free Software Foundation; either version 2, or (at your option) any
  10.    later version.
  11.  
  12.    This library is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this library; see the file COPYING. If not, write to the
  19.    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  20.    MA 02110-1301, USA.
  21.  
  22.    As a special exception, if you link this library with other files,
  23.    some of which are compiled with SDCC, to produce an executable,
  24.    this library does not by itself cause the resulting executable to
  25.    be covered by the GNU General Public License. This exception does
  26.    not however invalidate any other reasons why the executable file
  27.    might be covered by the GNU General Public License.
  28. -------------------------------------------------------------------------*/
  29.  
  30. /*KA******************************************************************
  31. * PROJECT: PL-One/8052
  32. **********************************************************************
  33. * FILE: ser.c
  34. **********************************************************************
  35. * CHANGES:
  36. * date      author            description
  37. * --------------------------------------------------------------------
  38. * 04/26/99  we                final
  39. * 04/27/99  we                comments
  40. **********************************************************************
  41. * DESCRIPTION:
  42. * This file contains a simple interrupt driven serial driver with
  43. * buffer (no check for overflow!!!).
  44. **********************************************************************
  45. * FUNCTIONS DECLARED:
  46. * ser_init       Initialization; must be called first
  47. * ser_putc       output one char on the serial line
  48. * ser_getc       return a char if one has been received, else 0
  49. * ser_printString print a 0-terminated string
  50. * ser_charAvail  return 1 if a char arrived on serial line
  51. **********************************************************************
  52. * NOTE:
  53. * Remember to enable all interrupts (EA=1) outside of this module!!
  54. **********************************************************************
  55. * COMPILE TIME OPTIONS: -
  56. * DEBUG OPTIONS: -
  57. ******************************************************************KE*/
  58.  
  59. #include <8052.h>
  60.  
  61. #include "ser.h"
  62.  
  63. #define NON_BLOCKING
  64.  
  65. unsigned char __xdata ser_txIndexIn;
  66. unsigned char __xdata ser_txIndexOut;
  67. unsigned char __xdata ser_rxIndexIn;
  68. unsigned char __xdata ser_rxIndexOut;
  69.  
  70. unsigned char __xdata ser_txBuffer[0x100];
  71. unsigned char __xdata ser_rxBuffer[0x100];
  72.  
  73. static __bit ser_txBusy;
  74.  
  75. void
  76. ser_init(void)
  77. {
  78.   ES = 0;
  79.  
  80.   ser_txBusy     = 0;
  81.  
  82.   ser_txIndexIn  = 0;
  83.   ser_txIndexOut = 0;
  84.   ser_rxIndexIn  = 0;
  85.   ser_rxIndexOut = 0;
  86.  
  87.   T2CON = 0x30;
  88.  
  89.   /* Baudrate = 19200, oscillator frq. of my processor is 21.4772 MHz */
  90.   RCAP2H = 0xFF;
  91.   RCAP2L = 0xDD;
  92.  
  93.   /* enable counter */
  94.   T2CON = 0x34;
  95.  
  96.   SCON = 0x50;
  97.  
  98.   if (TI) {
  99.     TI = 0;
  100.   }
  101.   if (RI) {
  102.     RI = 0;
  103.   }
  104.  
  105.   ES=1;  
  106. }
  107.  
  108. void
  109. ser_interrupt_handler(void) __interrupt 4 __using 1
  110. {
  111.   ES=0;
  112.  
  113.   if (RI) {
  114.     RI = 0;
  115.     ser_rxBuffer[ser_rxIndexIn++] = SBUF;
  116.   }
  117.  
  118.   if (TI) {
  119.     TI = 0;
  120.     if (ser_txIndexIn == ser_txIndexOut) {
  121.       ser_txBusy = 0;
  122.     }
  123.     else {
  124.       SBUF = ser_txBuffer[ser_txIndexOut++];
  125.     }
  126.   }
  127.  
  128.   ES=1;
  129. }
  130.  
  131. void
  132. ser_putc(unsigned char c)
  133. {
  134.   ES=0;
  135.  
  136.   if (ser_txBusy) {
  137.     ser_txBuffer[ser_txIndexIn++] = c;
  138.   }
  139.   else {
  140.     ser_txBusy = 1;
  141.     SBUF = c;
  142.   }
  143.  
  144.   ES=1;
  145. }
  146.  
  147. unsigned char
  148. ser_getc(void)
  149. {
  150.   char tmp;
  151.  
  152. #ifdef NON_BLOCKING
  153.   if (ser_rxIndexIn != ser_rxIndexOut) {
  154.     tmp = ser_rxBuffer[ser_rxIndexOut++];
  155.   }
  156.   else {
  157.     tmp = 0;
  158.   }
  159. #endif
  160.  
  161.   return(tmp);
  162. }
  163.  
  164. void
  165. ser_printString(char *String)
  166. {
  167.   while (*String) {
  168.     ser_putc(*String++);
  169.   }
  170. }
  171.  
  172. char
  173. ser_charAvail(void)
  174. {
  175.   char ret = 0;
  176.  
  177.   if (ser_rxIndexIn != ser_rxIndexOut) {
  178.     ret = 1;
  179.   }
  180.  
  181.   return(ret);
  182. }
  183.  
  184. /*********************End of File************************************/
  185.