?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /*-------------------------------------------------------------------------
  2.    serial.c - this module implements serial interrupt handler and IO
  3.               routinwes using two 256 byte cyclic buffers. Bit variables
  4.               can be used as flags for real-time kernel tasks
  5.  
  6.    Copyright (C) 1996, Dmitry S. Obukhov <dmitry.obukhov AT gmail.com>
  7.  
  8.    This library is free software; you can redistribute it and/or modify it
  9.    under the terms of the GNU General Public License as published by the
  10.    Free Software Foundation; either version 2, or (at your option) any
  11.    later version.
  12.  
  13.    This library is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this library; see the file COPYING. If not, write to the
  20.    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  21.    MA 02110-1301, USA.
  22.  
  23.    As a special exception, if you link this library with other files,
  24.    some of which are compiled with SDCC, to produce an executable,
  25.    this library does not by itself cause the resulting executable to
  26.    be covered by the GNU General Public License. This exception does
  27.    not however invalidate any other reasons why the executable file
  28.    might be covered by the GNU General Public License.
  29. -------------------------------------------------------------------------*/
  30.  
  31. /* This module contains definition of I8051 registers */
  32. #include "8052.h"
  33.  
  34.  
  35. static unsigned char __xdata stx_index_in, srx_index_in, stx_index_out, srx_index_out;
  36. static unsigned char __xdata stx_buffer[0x100];
  37. static unsigned char __xdata srx_buffer[0x100];
  38.  
  39. static __bit work_flag_byte_arrived;
  40. static __bit work_flag_buffer_transfered;
  41. static __bit tx_serial_buffer_empty;
  42. static __bit rx_serial_buffer_empty;
  43.  
  44.  
  45. void serial_init(void)
  46. {
  47.     SCON = 0x50;
  48.     T2CON = 0x34;
  49.     PS = 1;
  50.     T2CON = 0x34;
  51.     RCAP2H = 0xFF;
  52.     RCAP2L = 0xDA;
  53.    
  54.     RI = 0;
  55.     TI = 0;
  56.    
  57.     stx_index_in = srx_index_in = stx_index_out = srx_index_out = 0;
  58.     rx_serial_buffer_empty = tx_serial_buffer_empty = 1;
  59.     work_flag_buffer_transfered = 0;
  60.     work_flag_byte_arrived = 0;
  61.     ES=1;
  62. }
  63.  
  64. void serial_interrupt_handler(void) __interrupt 4 __using 1
  65. {
  66.     ES=0;
  67.     if ( RI )
  68.         {
  69.             RI = 0;
  70.             srx_buffer[srx_index_in++]=SBUF;
  71.             work_flag_byte_arrived = 1;
  72.             rx_serial_buffer_empty = 0;
  73.         }
  74.     if ( TI )
  75.         {
  76.             TI = 0;
  77.             if (stx_index_out == stx_index_in )
  78.                 {
  79.                     tx_serial_buffer_empty = 1;
  80.                     work_flag_buffer_transfered = 1;
  81.                 }
  82.             else SBUF = stx_buffer[stx_index_out++];
  83.         }
  84.     ES=1;
  85. }
  86.  
  87. /* Next two functions are interface */
  88.  
  89. void serial_putc(unsigned char c)
  90. {
  91.     stx_buffer[stx_index_in++]=c;
  92.     ES=0;
  93.     if ( tx_serial_buffer_empty )
  94.         {
  95.             tx_serial_buffer_empty = 0;
  96.             TI=1;
  97.         }
  98.     ES=1;
  99. }
  100.  
  101. unsigned char serial_getc(void)
  102. {
  103.     unsigned char tmp = srx_buffer[srx_index_out++];
  104.     ES=0;
  105.     if ( srx_index_out == srx_index_in) rx_serial_buffer_empty = 1;
  106.     ES=1;
  107.     return tmp;
  108. }
  109.