Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "APM_FastSerial.h"
00023 extern "C" {
00024
00025 #include <inttypes.h>
00026 #include <avr/interrupt.h>
00027 #include <avr/io.h>
00028 #include "WConstants.h"
00029 }
00030 #define TX_BUFFER_SIZE 80 // Serial output buffer size
00031
00032
00033 uint8_t tx_buffer0[TX_BUFFER_SIZE];
00034 volatile int tx_buffer0_head=0;
00035 volatile int tx_buffer0_tail=0;
00036
00037 #if defined(__AVR_ATmega1280__)
00038
00039 uint8_t tx_buffer3[TX_BUFFER_SIZE];
00040 volatile int tx_buffer3_head=0;
00041 volatile int tx_buffer3_tail=0;
00042 #endif
00043
00044 #if defined(__AVR_ATmega1280__) // For atmega1280 we use Serial port 0 and 3
00045
00046 ISR(SIG_USART0_DATA)
00047 {
00048 uint8_t data;
00049
00050 if (tx_buffer0_tail == tx_buffer0_head)
00051 UCSR0B &= ~(_BV(UDRIE0));
00052 else {
00053 data = tx_buffer0[tx_buffer0_tail];
00054 tx_buffer0_tail = (tx_buffer0_tail + 1) % TX_BUFFER_SIZE;
00055 UDR0 = data;
00056 }
00057 }
00058
00059
00060 ISR(SIG_USART3_DATA)
00061 {
00062 uint8_t data;
00063
00064 if (tx_buffer3_tail == tx_buffer3_head)
00065 UCSR3B &= ~(_BV(UDRIE3));
00066 else {
00067 data = tx_buffer3[tx_buffer3_tail];
00068 tx_buffer3_tail = (tx_buffer3_tail + 1) % TX_BUFFER_SIZE;
00069 UDR3 = data;
00070 }
00071 }
00072 #else
00073
00074
00075 ISR(USART_UDRE_vect)
00076 {
00077 uint8_t data;
00078
00079 if (tx_buffer0_tail == tx_buffer0_head)
00080 UCSR0B &= ~(_BV(UDRIE0));
00081 else {
00082 data = tx_buffer0[tx_buffer0_tail];
00083 tx_buffer0_tail = (tx_buffer0_tail + 1) % TX_BUFFER_SIZE;
00084 UDR0 = data;
00085 }
00086 }
00087 #endif
00088
00089
00090 APM_FastSerial_Class::APM_FastSerial_Class(uint8_t SerialPort)
00091 {
00092 SerialPortNumber=SerialPort;
00093 }
00094
00095
00096
00097
00098 void APM_FastSerial_Class::write(uint8_t b)
00099 {
00100 uint8_t Enable_tx_int=0;
00101 uint8_t new_head;
00102
00103 if (SerialPortNumber==0)
00104 {
00105
00106 if (tx_buffer0_tail == tx_buffer0_head)
00107 Enable_tx_int=1;
00108
00109 new_head = (tx_buffer0_head + 1) % TX_BUFFER_SIZE;
00110 if (new_head==tx_buffer0_tail)
00111 return;
00112
00113 tx_buffer0[tx_buffer0_head] = b;
00114 tx_buffer0_head = new_head;
00115
00116 if (Enable_tx_int)
00117 UCSR0B |= _BV(UDRIE0);
00118 }
00119 #if defined(__AVR_ATmega1280__)
00120 else
00121 {
00122
00123 if (tx_buffer3_tail == tx_buffer3_head)
00124 Enable_tx_int=1;
00125
00126 new_head = (tx_buffer3_head + 1) % TX_BUFFER_SIZE;
00127 if (new_head==tx_buffer3_tail)
00128 return;
00129
00130 tx_buffer3[tx_buffer3_head] = b;
00131 tx_buffer3_head = new_head;
00132
00133 if (Enable_tx_int)
00134 UCSR3B |= _BV(UDRIE3);
00135 }
00136 #endif
00137 }
00138
00139
00140 void APM_FastSerial_Class::write(const uint8_t *buffer, int size)
00141 {
00142 while (size--)
00143 write(*buffer++);
00144 }
00145
00146
00147 APM_FastSerial_Class APM_FastSerial(0);
00148 #if defined(__AVR_ATmega1280__)
00149 APM_FastSerial_Class APM_FastSerial3(3);
00150 #endif