#include #include #include #include #include #include #include "tb386ex.h" /* 8259 registers */ #define IRS OCW2MDOS /* Interrupt Controller */ #define EOI 0x20 /* end of interrupt command */ typedef unsigned int WORD; typedef unsigned char BYTE; #define CHAR_LEN_8 0x03 /* ------11 */ #define CHAR_LEN_7 0x02 /* ------10 */ #define STOP_LEN_1 0x00 /* -----0-- */ #define STOP_LEN_2 0x04 /* -----1-- */ #define NO_PARITY 0x00 /* ----0--- */ #define EVEN_PARITY 0x18 #define ODD_PARITY 0x08 #define TB386EX_BAUD9600 52 #define TB386EX_BAUD19200 26 #define TB386EX_BAUD38400 13 #define PORT_address 0x3F8 /* Com1 */ #define THR PORT_address + 0 #define BAR_LED 0x203 /* Bar LED On Board*/ #define ON 1 #define OFF 0 /*-----------------------------------------------------------------*/ void SetupCOM1_POLLING(WORD speed) { disable(); outportb(IER0DOS, 0x0); /* No Interrupt */ outportb(LCR0DOS, 0x80); /* for DLL Access */ outport(DLL0DOS, speed); /* Baudrate - WORD ! - */ outportb(LCR0DOS, (NO_PARITY|CHAR_LEN_8|STOP_LEN_1)); inportb(RBR0DOS); /* Readout Garbage */ enable(); } /*-----------------------------------------------------------------*/ void RestoreSerial(void) { while (!(inportb(LSR0DOS) & 0x40)) /* Drain TX Data */ ; disable(); outportb(IER0DOS, 0x01); /* RX Interrupt */ outportb(LCR0DOS, 0x80); /* for DLL Access */ outport(DLL0DOS, TB386EX_BAUD38400); /* 38400 bps */ outportb(LCR0DOS,(NO_PARITY|CHAR_LEN_8|STOP_LEN_1)); inportb(RBR0DOS); /* Readout Garbage */ enable(); } /*-----------------------------------------------------------------*/ int CheckRxByPolling(void) { return inportb(LSR0DOS) & 0x01; } /*-----------------------------------------------------------------*/ BYTE GetOneByteByPolling(void) { while (!CheckRxByPolling()) ; return inportb(RBR0DOS); } /*-----------------------------------------------------------------*/ void PutOneByteByPolling(BYTE ch) { while (!(inportb(LSR0DOS) & 0x20)) /* Drain TX Data */ ; outportb(THR, ch); } SendStringByPolling(char *s) { int i; for(i = 0 ; i < strlen(s) ; i++) { PutOneByteByPolling(s[i]); } } /*volatile int RxReady = 0; volatile BYTE RxData;*/ #define MAX_BUF_SIZE 20 volatile BYTE rcv_buf[MAX_BUF_SIZE]; volatile BYTE send_buf[MAX_BUF_SIZE]; volatile int rcv_first; volatile int rcv_last; volatile int send_first; volatile int send_last; volatile int flag = 0; void interrupt NewSerialISR() { while (!(inportb(IIR0DOS) & 0x01)) { /* INT Pending ? */ /* Receive Data Available */ if (inportb(LSR0DOS) & 0x01) { /* Rx Done ? */ if((rcv_last + 1) % MAX_BUF_SIZE != rcv_first) { rcv_buf[rcv_last] = inportb(RBR0DOS); rcv_last = (rcv_last + 1) % MAX_BUF_SIZE; outportb(IER0DOS,0x03); } else { inportb(RBR0DOS); } } if ( (inportb(LSR0DOS) & 0x20) ){ if (send_first != send_last){ outportb(THR0DOS,send_buf[send_first]); send_first = (send_first + 1) % MAX_BUF_SIZE; }else{ flag = 0; } } } outportb(IRS, EOI); } /* int CheckRxByInterrupt(void) { return RxReady; } /*-----------------------------------------------------------------*/ BYTE GetOneByteByInterrupt(void) { BYTE data; while(rcv_first == rcv_last); data = rcv_buf[rcv_first]; rcv_first = (rcv_first + 1) % MAX_BUF_SIZE; return data; } void PutOneByteByInterrupt(BYTE ch) { while((send_last+1) % MAX_BUF_SIZE == send_first) ; send_buf[send_last] = ch; if(flag == 0) { flag = 1; outportb(THR0DOS,ch); return; } send_last = (send_last+1) % MAX_BUF_SIZE; } /*-----------------------------------------------------------------------*/ void interrupt (far *sio_org)(); void SetupCOM1_INTERRUPT(void) { /* All other Serial Parameter is the Same as DOS */ disable(); sio_org = getvect(0x0C); /* Save Old Vector */ setvect(0x0C, NewSerialISR); enable(); } void RestoreCOM1_INTERRUPT() { disable(); outportb(IER0DOS, 0x01); /* RX INT */ setvect(0x0C, sio_org); inportb(RBR0DOS); /* Readout Garbage */ enable(); } void Exam1_A() { char ch; int ledvalue = OFF; printf("Exam1_A입니다.\n"); SetupCOM1_POLLING(TB386EX_BAUD38400); SendStringByPolling("Microprocessor 5 TEAM"); printf("\n"); while(1) { ch = GetOneByteByPolling(); if(ledvalue == ON) { ledvalue = OFF; outportb(BAR_LED, 0x7F); } else if (ledvalue == OFF) { ledvalue = ON; outportb(BAR_LED, 0xFE); } if(ch >= 'a' && ch <= 'z') { ch=toupper(ch); PutOneByteByPolling(ch); } else if(ch >= 'A' && ch <= 'Z') { ch=tolower(ch); PutOneByteByPolling(ch); } else if(ch == 13) { printf("\n"); } else if(ch == '~') { break; } else { PutOneByteByPolling(ch); } } RestoreSerial(); } void Exam1_B() { char ch; int ledvalue = OFF; printf("\nExam1_B입니다.\n"); printf("먼저 x232 통신설정을 9600Bps로 변환하십시요.\n"); while(!kbhit()) ; SetupCOM1_POLLING(TB386EX_BAUD9600); printf("현재 9600Bps 모드입니다.\n"); while(1) { ch = GetOneByteByPolling(); if(ledvalue == ON) { ledvalue = OFF; outportb(BAR_LED, 0x7F); } else if (ledvalue == OFF) { ledvalue = ON; outportb(BAR_LED, 0xFE); } if(ch >= 'a' && ch <= 'z') { ch=toupper(ch); PutOneByteByPolling(ch); } else if(ch >= 'A' && ch <= 'Z') { ch=tolower(ch); PutOneByteByPolling(ch); } else if(ch == 13) { printf("\n"); } else if(ch == '~') { printf("\n먼저 x232 통신설정을 38400Bps로 변환하십시요.\n"); while(!kbhit()) ; RestoreSerial(); break; } else { PutOneByteByPolling(ch); } } } void Exam1_C() { int loop; char ch; SetupCOM1_POLLING(TB386EX_BAUD38400); ch = GetOneByteByPolling(); printf("\nExam1_C입니다.\n"); while(1) { ch = GetOneByteByPolling(); if(ch == '~') break; for(loop = 0; loop < 2000; loop++) { PutOneByteByPolling(ch); } } RestoreSerial(); } void Exam2_A() { char ch; int ledvalue = OFF; printf("Exam2_A입니다.\n"); printf("\n"); while(1) { ch = GetOneByteByInterrupt(); if(ledvalue == ON) { ledvalue = OFF; outportb(BAR_LED, 0x7F); } else if (ledvalue == OFF) { ledvalue = ON; outportb(BAR_LED, 0xFE); } if(ch >= 'a' && ch <= 'z') { ch=toupper(ch); PutOneByteByInterrupt(ch); } else if(ch >= 'A' && ch <= 'Z') { ch=tolower(ch); PutOneByteByInterrupt(ch); } else if(ch == 13) { printf("\n"); } else if(ch == '~') { break; } else { PutOneByteByInterrupt(ch); } } } void Exam2_B() { int loop; char ch; printf("\nExam2_B입니다.\n"); while(1) { ch = GetOneByteByInterrupt(); if(ch == '~') break; for(loop = 0; loop < 2000; loop++) { PutOneByteByInterrupt(ch); } } } void main(void) { Exam1_A(); Exam1_B(); Exam1_C(); SetupCOM1_INTERRUPT(); Exam2_A(); Exam2_B(); RestoreCOM1_INTERRUPT(); }