USB-BDI
M:/USB-BDI/USB-BDI/Firmware/MPC555-USB-BDI/src/uart.c
Go to the documentation of this file.
00001 /*      USB-BDI Used for programming and debugging a MPC555 over USB
00002  * 
00003  *  Copyright 2005 NTB Interstate University of Applied Sciences of Technology Buchs
00004  *
00005  *  This program is free software: you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation, either version 3 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  */
00015 
00025 #include "isr.h"
00026 #include "dispatch.h"
00027 #include "usbpdi_common.h"
00028 #include "usbpdi_main.h"
00029 #include "UART.h"
00030 #include "fx2regs.h"
00031 #include "delay.h"
00032 
00034 volatile unsigned short uart0OutBufferStart = 0;
00036 volatile unsigned short uart0OutBufferEnd = 0;
00038 volatile unsigned short uart0InBufferStart = 0;
00040 volatile unsigned short uart0InBufferEnd = 0;
00042 __xdata volatile unsigned char uart0OutBuffer[UART_BUF_LEN];
00044 __xdata volatile unsigned char uart0InBuffer[UART_BUF_LEN];
00047 unsigned char tmpBuffer;
00048 
00049 
00054 static void isr_uart0(void) __interrupt{
00055         unsigned short uart0InBufferLen;
00056 
00057         if (RI) {       // character has been received
00058                 RI = 0;
00059                 // write data to buffer
00060                 uart0OutBuffer[uart0OutBufferEnd] = SBUF0;
00061                 uart0OutBufferEnd = (uart0OutBufferEnd + 1) % UART_BUF_LEN;
00062                 // move the start position if the length is at the maximum
00063                 // thats to overwrite the oldest data (at the start), the length will not change
00064                 if (uart0OutBufferEnd == uart0OutBufferStart){
00065                         uart0OutBufferStart = (uart0OutBufferStart + 1) % UART_BUF_LEN;
00066                 }
00067         }
00068         if (TI) {       // character has been sent
00069                 TI = 0;
00070                 Uart0TriggerSend();
00071         }
00072 }
00073 /*
00074 void isr_uart1(void) __interrupt
00075 {
00076         if (RI1) {      // character has been received
00077                 // buffer = SBUF1;
00078                 RI1 = 0;
00079                 // SBUF1 = buffer;
00080         }
00081         if (TI1) {      // character has been sent
00082                 // SBUF1 = buffer;
00083                 TI1 = 0;
00084         }
00085 }
00086 */
00087 
00093 void initUART()
00094 {
00095         // Timer 1, Baud Rate = 9600bps
00096         PCON |= 0x80;                   // baud rate doubler enable (SMOD0)
00097         SMOD1 = 1;                              // baud rate doubler enable (SMOD1)
00098         CKCON |= 0x10;                  // (CKCON.4) Timer 1 uses CLKOUT/4
00099         TMOD |= 0x20;                   // Mode 2 for Timer 1
00100         TH1 = 0xB2;                             // set baud rate (9600bps)
00101         TR1 = 1;                                // enable Timer 1
00102         // Serial Port 0
00103         SM0 = 0; SM1 = 1;       // Mode 1
00104         REN = 1;                        // receive enabled
00105         hook_sv(SV_SERIAL_0, (unsigned short) isr_uart0);       // install interrupt handler
00106         TI = 0;                         // clear transmit interrupt
00107         tmpBuffer = SBUF0;              // empty the buffer
00108         RI = 0;                         // clear receive interrupt
00109         ES0 = 1;                        // Enable Serial Port 0 Interrupts
00110         /*
00111         // Serial Port 1
00112         SM01 = 0; SM11 = 1;     // Mode 1
00113         REN1 = 1;                       // receive enabled=1 disable=0
00114         //hook_sv(SV_SERIAL_1, (unsigned short) isr_uart1);     // install interrupt handler
00115         TI1 = 0;                        // clear transmit interrupt
00116         tmpBuffer = SBUF1;      // empty the buffer
00117         RI1 = 0;                        // clear receive interrupt
00118         //ES1 = 1;                      // Enable Serial Port 1 Interrupts
00119         ES1 = 0;                        // Disable Interrupts for debuging
00120         */
00121 }
00135 void changeBaudRate(unsigned long bitRate){
00136         TR1 = 0; //disable timer 1
00137         TH1 = 255-750000/bitRate;
00138         TR1 = 1;//enable timer 1
00139 }
 All Files Functions Variables Defines