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

Serial Port (RS232)
[Device Drivers]


Functions

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


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

The RS232 COM-port driver.

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 }

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.

Definition at line 247 of file serial.c.

00248 {
00249         static char *default_bps = "38400";
00250         task_t *child;
00251         word key;
00252 
00253         kprintf(        "\n\rOpening RS232-1 interface (COM1)... "
00254                 "\n\rPress CTRL-X to quit."     );
00255 
00256         if (strcmp(argv, "2400")==0)            init_rs232(COM1, BPS_2400);
00257         else if (strcmp(argv, "4800")==0)       init_rs232(COM1, BPS_4800);
00258         else if (strcmp(argv, "9600")==0)       init_rs232(COM1, BPS_9600);
00259         else if (strcmp(argv, "19200")==0)      init_rs232(COM1, BPS_19200);
00260         else if (strcmp(argv, "38400")==0)      init_rs232(COM1, BPS_38400);
00261         else if (strcmp(argv, "56700")==0)      init_rs232(COM1, BPS_56700);
00262         else if (strcmp(argv, "115200")==0)     init_rs232(COM1, BPS_115200);
00263         else
00264         {
00265                 // Default bps //
00266                 argv = default_bps;
00267                 init_rs232(COM1, BPS_38400);
00268         }
00269 
00270         kprintf("\n\rBitrate: %s bps", argv);
00271         child = create_process(&rs232_task, &rs232_task, 0, "rs232_task", KERNEL_PRIVILEGE);
00272         set_color(GREEN);
00273         kprintf("\r                                             [ OK ]\n\r");
00274         set_color(DEFAULT_COLOR);
00275 
00276         while(TRUE)
00277         {
00278                 // Get a char from the keyboard //
00279                 key = kgetchar();
00280                 if (key==CTRL_X) break;
00281                 if ((key&0xFF)==13) rs232_putchar(10);
00282 
00283                 // Write the char to the serial port //
00284                 rs232_putchar(key);
00285         }
00286 
00287         // Local echo of the ESCape char //
00288         kputchar((byte)CTRL_X);
00289 
00290         // Kill the rs232 task //
00291         kill(child->pid);
00292 
00293         // Close RS232 connection //
00294         close_rs232(curr_port);
00295         kprintf("\n\rBye.\n\r");
00296 }

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 }

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).

Definition at line 235 of file serial.c.

00236 {
00237         while(TRUE)
00238         {
00239                 kputchar(rs232_getchar());
00240         }
00241 }


Variable Documentation

word curr_port = COM1 [static]
 

RS232 current port.

Definition at line 43 of file serial.c.

byte rs232_read_buf[RS232_BUF_DIM]
 

RS232 buffer for read operations.

Definition at line 32 of file serial.c.

int rs232_read_head = 0 [static]
 

RS232 buffer for read operations :: head pointer.

Definition at line 27 of file serial.c.

int rs232_read_tail = 0 [static]
 

RS232 buffer for read operations :: tail pointer.

Definition at line 29 of file serial.c.

byte rs232_write_buf[RS232_BUF_DIM]
 

RS232 buffer for write operations.

Definition at line 40 of file serial.c.

int rs232_write_head = 0 [static]
 

RS232 buffer for write operations :: head pointer.

Definition at line 35 of file serial.c.

int rs232_write_tail = 0 [static]
 

RS232 buffer for write operations :: tail pointer.

Definition at line 37 of file serial.c.


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