4 changed files with 0 additions and 268 deletions
@ -1,150 +0,0 @@
@@ -1,150 +0,0 @@
|
||||
/*
|
||||
APM_FastSerial.cpp - Fast Serial Output for Ardupilot Mega Hardware (atmega1280) |
||||
It´s also compatible with standard Arduino boards (atmega 168 and 328) |
||||
Interrupt driven Serial output with intermediate buffer |
||||
Code Jose Julio and Jordi Muñoz. DIYDrones.com |
||||
|
||||
This library is free software; you can redistribute it and/or |
||||
modify it under the terms of the GNU Lesser General Public |
||||
License as published by the Free Software Foundation; either |
||||
version 2.1 of the License, or (at your option) any later version. |
||||
|
||||
This library works as a complement of the standard Arduino Serial |
||||
library. So user must initialize Standard Serial Arduino library first. |
||||
This library works in Serial port 0 and Serial port3(telemetry port)[APM] |
||||
Methods: (the same as standard arduino library, inherits from Print) |
||||
write() for bytes or array of bytes (binary output) |
||||
print() for chars, strings, numbers and floats |
||||
println() |
||||
*/ |
||||
|
||||
//#include "WProgram.h"
|
||||
#include "APM_FastSerial.h" |
||||
extern "C" { |
||||
// AVR LibC Includes
|
||||
#include <inttypes.h> |
||||
#include <avr/interrupt.h> |
||||
#include <avr/io.h> |
||||
#include "WConstants.h" |
||||
} |
||||
#define TX_BUFFER_SIZE 80 // Serial output buffer size
|
||||
|
||||
// Serial0 buffer
|
||||
uint8_t tx_buffer0[TX_BUFFER_SIZE]; |
||||
volatile int tx_buffer0_head=0; |
||||
volatile int tx_buffer0_tail=0; |
||||
|
||||
#if defined(__AVR_ATmega1280__) |
||||
// Serial3 buffer
|
||||
uint8_t tx_buffer3[TX_BUFFER_SIZE]; |
||||
volatile int tx_buffer3_head=0; |
||||
volatile int tx_buffer3_tail=0; |
||||
#endif |
||||
|
||||
#if defined(__AVR_ATmega1280__) // For atmega1280 we use Serial port 0 and 3
|
||||
// Serial0 interrupt
|
||||
ISR(SIG_USART0_DATA) |
||||
{ |
||||
uint8_t data; |
||||
|
||||
if (tx_buffer0_tail == tx_buffer0_head) |
||||
UCSR0B &= ~(_BV(UDRIE0)); // Disable interrupt
|
||||
else { |
||||
data = tx_buffer0[tx_buffer0_tail]; |
||||
tx_buffer0_tail = (tx_buffer0_tail + 1) % TX_BUFFER_SIZE;
|
||||
UDR0 = data; |
||||
} |
||||
} |
||||
|
||||
// Serial3 interrupt
|
||||
ISR(SIG_USART3_DATA) |
||||
{ |
||||
uint8_t data; |
||||
|
||||
if (tx_buffer3_tail == tx_buffer3_head)
|
||||
UCSR3B &= ~(_BV(UDRIE3)); // Disable interrupt
|
||||
else { |
||||
data = tx_buffer3[tx_buffer3_tail]; |
||||
tx_buffer3_tail = (tx_buffer3_tail + 1) % TX_BUFFER_SIZE;
|
||||
UDR3 = data; |
||||
} |
||||
} |
||||
#else |
||||
|
||||
// Serial interrupt
|
||||
ISR(USART_UDRE_vect) |
||||
{ |
||||
uint8_t data; |
||||
|
||||
if (tx_buffer0_tail == tx_buffer0_head) |
||||
UCSR0B &= ~(_BV(UDRIE0)); // Disable interrupt
|
||||
else { |
||||
data = tx_buffer0[tx_buffer0_tail]; |
||||
tx_buffer0_tail = (tx_buffer0_tail + 1) % TX_BUFFER_SIZE;
|
||||
UDR0 = data; |
||||
} |
||||
} |
||||
#endif |
||||
|
||||
// Constructors ////////////////////////////////////////////////////////////////
|
||||
APM_FastSerial_Class::APM_FastSerial_Class(uint8_t SerialPort) |
||||
{ |
||||
SerialPortNumber=SerialPort; // This could be serial port 0 or 3
|
||||
} |
||||
|
||||
// Public Methods //////////////////////////////////////////////////////////////
|
||||
|
||||
// This is the important function (basic funtion: send a byte)
|
||||
void APM_FastSerial_Class::write(uint8_t b) |
||||
{ |
||||
uint8_t Enable_tx_int=0; |
||||
uint8_t new_head; |
||||
|
||||
if (SerialPortNumber==0) // Serial Port 0
|
||||
{ |
||||
// if buffer was empty then we enable Serial TX interrupt
|
||||
if (tx_buffer0_tail == tx_buffer0_head) |
||||
Enable_tx_int=1; |
||||
|
||||
new_head = (tx_buffer0_head + 1) % TX_BUFFER_SIZE; // Move to next position in the ring buffer
|
||||
if (new_head==tx_buffer0_tail) |
||||
return; // This is an Error : BUFFER OVERFLOW. We lost this character!!
|
||||
|
||||
tx_buffer0[tx_buffer0_head] = b; // Add data to the tx buffer
|
||||
tx_buffer0_head = new_head; // Update head pointer
|
||||
|
||||
if (Enable_tx_int) |
||||
UCSR0B |= _BV(UDRIE0); // Enable Serial TX interrupt
|
||||
} |
||||
#if defined(__AVR_ATmega1280__) |
||||
else // Serial Port 3
|
||||
{ |
||||
// if buffer was empty then we enable Serial TX interrupt
|
||||
if (tx_buffer3_tail == tx_buffer3_head) |
||||
Enable_tx_int=1; |
||||
|
||||
new_head = (tx_buffer3_head + 1) % TX_BUFFER_SIZE; // Move to next position in the ring buffer
|
||||
if (new_head==tx_buffer3_tail) |
||||
return; // This is an Error : BUFFER OVERFLOW. We lost this character!!
|
||||
|
||||
tx_buffer3[tx_buffer3_head] = b; // Add data to the tx buffer
|
||||
tx_buffer3_head = new_head; // Update head pointer
|
||||
|
||||
if (Enable_tx_int) |
||||
UCSR3B |= _BV(UDRIE3); // Enable Serial TX interrupt
|
||||
} |
||||
#endif |
||||
} |
||||
|
||||
// Send a buffer of bytes (this is util for binary protocols)
|
||||
void APM_FastSerial_Class::write(const uint8_t *buffer, int size) |
||||
{ |
||||
while (size--) |
||||
write(*buffer++); |
||||
} |
||||
|
||||
// We create this two instances
|
||||
APM_FastSerial_Class APM_FastSerial(0); // For Serial port 0
|
||||
#if defined(__AVR_ATmega1280__) |
||||
APM_FastSerial_Class APM_FastSerial3(3); // For Serial port 3 (only Atmega1280)
|
||||
#endif |
@ -1,25 +0,0 @@
@@ -1,25 +0,0 @@
|
||||
#ifndef APM_FastSerial_h |
||||
#define APM_FastSerial_h |
||||
|
||||
#include <inttypes.h> |
||||
|
||||
#include "Print.h" |
||||
|
||||
class APM_FastSerial_Class : public Print // Inherit from Print
|
||||
{ |
||||
private:
|
||||
uint8_t SerialPortNumber; |
||||
|
||||
public: |
||||
APM_FastSerial_Class(uint8_t SerialPort); // Constructor
|
||||
// we overwrite the write methods
|
||||
void write(uint8_t b); // basic funtion : send a byte
|
||||
void write(const uint8_t *buffer, int size); |
||||
}; |
||||
|
||||
extern APM_FastSerial_Class APM_FastSerial; |
||||
#if defined(__AVR_ATmega1280__) |
||||
extern APM_FastSerial_Class APM_FastSerial3; |
||||
#endif |
||||
#endif |
||||
|
@ -1,91 +0,0 @@
@@ -1,91 +0,0 @@
|
||||
/* |
||||
Example of APM_FastSerial library. |
||||
Code by Jose Julio and Jordi Muñoz. DIYDrones.com |
||||
*/ |
||||
|
||||
// FastSerial is implemented for Serial port 0 (connection to PC) and 3 (telemetry) |
||||
#include <APM_FastSerial.h> // ArduPilot Mega Fast Serial Output Library |
||||
|
||||
#if defined(__AVR_ATmega1280__) |
||||
#define LED 35 |
||||
#else |
||||
#define LED 13 |
||||
#endif |
||||
|
||||
unsigned long timer; |
||||
unsigned long counter; |
||||
|
||||
void setup() |
||||
{ |
||||
int myint=14235; // Examples of data tytpes |
||||
long mylong=-23456432; |
||||
float myfloat=-26.669; |
||||
byte mybyte=0xD1; |
||||
byte bc_bufIn[50]; |
||||
|
||||
for (int i=0;i<10;i++) |
||||
bc_bufIn[i]=i*10+30; // we fill the byte array |
||||
|
||||
pinMode(LED,OUTPUT); |
||||
|
||||
// We use the standard serial port initialization |
||||
Serial.begin(57600); |
||||
//Serial3.begin(57600); // if we want to use port3 also (Mega boards)... |
||||
delay(100); |
||||
// We can use both methods to write to serial port: |
||||
Serial.println("ArduPilot Mega FastSerial library test"); // Standard serial output |
||||
APM_FastSerial.println("FAST_SERIAL : ArduPilot Mega FastSerial library test"); // Fast serial output |
||||
// We can use the same on serial port3 (telemetry) |
||||
// APM_FastSerial3.println("Serial Port3 : ArduPilot Mega FastSerial library test"); |
||||
delay(1000); |
||||
// Examples of data types (same result as standard arduino library) |
||||
APM_FastSerial.println("Numbers:"); |
||||
APM_FastSerial.println(myint); |
||||
APM_FastSerial.println(mylong); |
||||
APM_FastSerial.println(myfloat); |
||||
APM_FastSerial.println("Byte:"); |
||||
APM_FastSerial.write(mybyte); |
||||
APM_FastSerial.println(); |
||||
APM_FastSerial.println("Bytes:"); |
||||
APM_FastSerial.write(bc_bufIn,10); |
||||
APM_FastSerial.println(); |
||||
delay(4000); |
||||
} |
||||
|
||||
void loop() |
||||
{ |
||||
if((millis()- timer) > 20) // 50Hz loop |
||||
{ |
||||
timer = millis(); |
||||
if (counter < 250) // we use the Normal Serial output for 5 seconds |
||||
{ |
||||
// Example of typical telemetry output |
||||
digitalWrite(LED,HIGH); |
||||
Serial.println("!ATT:14.2;-5.5;-26.8"); // 20 bytes |
||||
digitalWrite(LED,LOW); |
||||
if ((counter%5)==0) // GPS INFO at 5Hz |
||||
{ |
||||
digitalWrite(LED,HIGH); |
||||
Serial.println("!LAT:26.345324;LON:-57.372638;ALT:46.7;GC:121.3;GS:23.1"); // 54 bytes |
||||
digitalWrite(LED,LOW); |
||||
} |
||||
} |
||||
else // and Fast Serial Output for other 5 seconds |
||||
{ |
||||
// The same info... |
||||
digitalWrite(LED,HIGH); |
||||
APM_FastSerial.println("#ATT:14.2;-5.5;-26.8"); // 20 bytes |
||||
digitalWrite(LED,LOW); |
||||
if ((counter%5)==0) // GPS INFO at 5Hz |
||||
{ |
||||
digitalWrite(LED,HIGH); |
||||
|
||||
APM_FastSerial.println("#LAT:26.345324;LON:-57.372638;ALT:46.7;GC:121.3;GS:23.1"); // 54 bytes |
||||
digitalWrite(LED,LOW); |
||||
} |
||||
if (counter>500) // Counter reset |
||||
counter=0; |
||||
} |
||||
counter++; |
||||
} |
||||
} |
Loading…
Reference in new issue