Functions | |||||
| void | rs232_handler (word port) | ||||
RS232 common handler routine.
| |||||
| void | close_rs232 (word port) | ||||
Close a specific RS232 port.
| |||||
| void | init_rs232 (word port, byte divisor) | ||||
Initialize the RS232 interface with a bps rate.
| |||||
| byte | rs232_getchar () | ||||
Get a character from the current serial port.
| |||||
| void | rs232_putchar (byte c) | ||||
Write a character to the current serial port.
| |||||
| void | rs232_task () | ||||
Get permanently characters from the serial port and print it to the screen.
| |||||
| void | rs232_chat (char *argv) | ||||
Chat with a modem or a serial device.
| |||||
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. | |||||
|
|
Close a specific RS232 port.
Definition at line 122 of file serial.c.
|
|
||||||||||||
|
Initialize the RS232 interface with a bps rate.
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 }
|
|
|
Chat with a modem or a serial device.
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 }
|
|
|
Get a character from the current serial port.
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 }
|
|
|
RS232 common handler routine.
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 }
|
|
|
Write a character to the current serial port.
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 }
|
|
|
Get permanently characters from the serial port and print it to the screen.
Definition at line 235 of file serial.c.
00236 {
00237 while(TRUE)
00238 {
00239 kputchar(rs232_getchar());
00240 }
00241 }
|
|
|
RS232 current port.
|
|
|
RS232 buffer for read operations.
|
|
|
RS232 buffer for read operations :: head pointer.
|
|
|
RS232 buffer for read operations :: tail pointer.
|
|
|
RS232 buffer for write operations.
|
|
|
RS232 buffer for write operations :: head pointer.
|
|
|
RS232 buffer for write operations :: tail pointer.
|
1.2.18