Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

serial.c File Reference

Serial/RS232 port driver. More...

#include <const.h>
#include <string.h>
#include <arch/i386.h>
#include <arch/interrupt.h>
#include <kernel/console.h>
#include <kernel/keyboard.h>
#include <kernel/task.h>
#include <kernel/serial.h>

Go to the source code of this file.

Functions

void rs232_handler (word port)
 RS232 common handler routine.
Parameters:
port  The serial port.


void rs232_handler_port1 ()
 The COM1 interrupt handler.

void rs232_handler_port2 ()
 The COM2 interrupt handler.

void close_rs232 (word port)
 Close a specific RS232 port.
Parameters:
port  The port to close.


void init_rs232 (word port, byte divisor)
 Initialize the RS232 interface with a bps rate.
Parameters:
port  The serial port to initialize.
divisor  The divisor to set the bps rate (see serial.h).


byte rs232_getchar ()
 Get a character from the current serial port.
Returns:
The ASCII code of the character read.


void rs232_putchar (byte c)
 Write a character to the current serial port.
Parameters:
c  The ASCII code of the character to write.


void rs232_task ()
 Get permanently characters from the serial port and print it to the screen.
Note:
This is no-exit function, you have to use it not as a simple procedure, but as a kernel-thread (see task.c).


void rs232_chat (char *argv)
 Chat with a modem or a serial device.
Parameters:
argv  A string that contains the bps rate to initialize the serial port connection.



Variables

int rs232_read_head = 0
 RS232 buffer for read operations :: head pointer.

int rs232_read_tail = 0
 RS232 buffer for read operations :: tail pointer.

byte rs232_read_buf [RS232_BUF_DIM]
 RS232 buffer for read operations.

int rs232_write_head = 0
 RS232 buffer for write operations :: head pointer.

int rs232_write_tail = 0
 RS232 buffer for write operations :: tail pointer.

byte rs232_write_buf [RS232_BUF_DIM]
 RS232 buffer for write operations.

word curr_port = COM1
 RS232 current port.


Detailed Description

Serial/RS232 port driver.

Author:
Andrea Righi <drizzt@inwind.it>
Date:
Last update: 2003-11-08
Note:
Copyright (©) 2003 Andrea Righi

Definition in file serial.c.


Function Documentation

void close_rs232 word    port
 

Close a specific RS232 port.

Parameters:
port  The port to close.

Definition at line 122 of file serial.c.

00123 {
00124         // Turn off rs232 interrupt //
00125         outportb(port+1, 0);
00126 
00127         // Disable the FIFO //
00128         outportb(port+2, 0);
00129 
00130         // Disconnect UART from the ICU //
00131         outportb(port+4, 0);
00132 }

void init_rs232 word    port,
byte    divisor
 

Initialize the RS232 interface with a bps rate.

Parameters:
port  The serial port to initialize.
divisor  The divisor to set the bps rate (see serial.h).

Definition at line 138 of file serial.c.

00139 {
00140         // Install the IRQ handler routine                              //
00141         if ( (port == COM1) || (port == COM3) )
00142                 install_irq_handler( RS232_1_IRQ, (void *)rs232_handler_port1 );
00143         else if ( (port == COM2) || (port == COM4) )
00144                 install_irq_handler( RS232_2_IRQ, (void *)rs232_handler_port2 );
00145         else
00146                 return;
00147 
00148         // Turn off rs232 interrupt //
00149         outportb(port+1, 0);
00150 
00151         // Set DLAB on //
00152         outportb(port+3, 0x80);
00153 
00154         // Set baud rate - Divisor Latch Low Byte //
00155         outportb(port, divisor);
00156         // Set baud rate - Divisor Latch High Byte //
00157         outportb(port+1, 0x00);
00158 
00159         // 8 bits, no parity, 1 stop-bit //
00160         outportb(port+3, 0x03);
00161 
00162         // FIFO Control Register //
00163         outportb(port+2, 0xC7);
00164 
00165         // Turn on DTR, RTS and OUT2 //
00166         outportb(port+4, 0x0B);
00167 
00168         // Enable interrupts when data is received //
00169         outportb(port+1, 0x01);
00170 
00171         // Clear receiver //
00172         (void)inportb(port);
00173         // Clear line status //
00174         (void)inportb(port+5);
00175         // Clear modem status //
00176         (void)inportb(port+6);
00177 }

byte rs232_getchar  
 

Get a character from the current serial port.

Returns:
The ASCII code of the character read.

Definition at line 183 of file serial.c.

00184 {
00185         byte temp;
00186         dword IF = GET_IF();
00187 
00188         // If the buffer is empty enable interrupts and wait... //
00189         while( rs232_read_head==rs232_read_tail )
00190         {
00191                 enable();
00192                 idle();
00193         }
00194 
00195         disable();
00196 
00197         // Update the RS232 buffer in mutual exclusion & get the char //
00198         temp = rs232_read_buf[rs232_read_head];
00199         rs232_read_head = (rs232_read_head+1)%RS232_BUF_DIM;
00200 
00201         SET_IF(IF);
00202 
00203         return(temp);
00204 }

void rs232_handler word    port
 

RS232 common handler routine.

Parameters:
port  The serial port.

Definition at line 47 of file serial.c.

00048 {
00049         byte flag, ch;
00050 
00051         while(TRUE)
00052         {
00053                 // Get a char from the serial port //
00054                 flag = inportb(port+2);
00055 
00056                 if (flag & 0x01) break;
00057 
00058                 switch(flag & 0x06)
00059                 {
00060                         // Modem status - clear intr by reading modem status reg //
00061                         case 0x00:
00062                         (void)inportb(port+6);
00063                         break;
00064 
00065                         // Write char - Send the char to the serial port //
00066                         case 0x02:
00067                         if (rs232_write_head != rs232_write_tail)
00068                         {
00069                                 ch = rs232_write_buf[rs232_write_head];
00070                                 rs232_write_head = (rs232_write_head+1)%RS232_BUF_DIM;
00071                                 outportb(port, ch);
00072                         }
00073                         // Inhibit TX interrupts if write buffer is empty (leave RX enabled) //
00074                         if (rs232_write_head == rs232_write_tail)
00075                                 outportb(port+1, 0x01);
00076                         break;
00077 
00078                         // Read char - Get the char from the serial port //
00079                         case 0x04:
00080                         while(TRUE)
00081                         {
00082                                 // Check for data ready //
00083                                 flag = inportb(port+5);
00084                                 if (!(flag & 0x01)) break;
00085 
00086                                 // Get the character //
00087                                 ch = inportb(port);
00088 
00089                                 // Store it into the buffer if it is not full //
00090                                 if (((rs232_read_tail+1)%RS232_BUF_DIM) != rs232_read_head)
00091                                 {
00092                                         rs232_read_buf[rs232_read_tail] = ch;
00093                                         rs232_read_tail = (rs232_read_tail+1)%RS232_BUF_DIM;
00094                                 }
00095                         }
00096                         break;
00097 
00098                         // Line status - clear intr by reading line status reg //
00099                         case 0x06:
00100                         (void)inportb(port+5);
00101                         break;
00102                 }
00103         }
00104 }

void rs232_putchar byte    c
 

Write a character to the current serial port.

Parameters:
c  The ASCII code of the character to write.

Definition at line 210 of file serial.c.

00211 {
00212         dword IF = GET_IF();
00213 
00214         // Check if the write buffer is full //
00215         if (((rs232_write_tail+1)%RS232_BUF_DIM) == rs232_write_head) return;
00216 
00217         disable();
00218 
00219         // Write the char to the buffer in mutual exclusion //
00220         rs232_write_buf[rs232_write_tail] = c;
00221         rs232_write_tail = (rs232_write_tail+1)%RS232_BUF_DIM;
00222 
00223         SET_IF(IF);
00224 
00225         // Enable RX & TX interrupts //
00226         outportb(curr_port+1, 0x03);
00227 }


Generated on Fri Feb 20 15:32:20 2004 for Minirighi by doxygen1.2.18