2012-09-14 14:58:55 -03:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "vprintf.h"
|
|
|
|
|
|
|
|
#include <AP_HAL.h>
|
|
|
|
#include "Console.h"
|
|
|
|
using namespace AP_HAL_AVR;
|
|
|
|
|
|
|
|
AVRConsoleDriver::AVRConsoleDriver() :
|
|
|
|
_user_backend(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// ConsoleDriver method implementations ///////////////////////////////////////
|
|
|
|
void AVRConsoleDriver::init(void* base_uart) {
|
|
|
|
_base_uart = (AP_HAL::UARTDriver*) base_uart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AVRConsoleDriver::backend_open() {
|
2012-09-14 21:07:28 -03:00
|
|
|
_txbuf.allocate(128);
|
|
|
|
_rxbuf.allocate(16);
|
2012-09-14 14:58:55 -03:00
|
|
|
_user_backend = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AVRConsoleDriver::backend_close() {
|
|
|
|
_user_backend = false;
|
|
|
|
}
|
|
|
|
|
2012-12-06 15:02:28 -04:00
|
|
|
size_t AVRConsoleDriver::backend_read(uint8_t *data, size_t len) {
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
int16_t b = _txbuf.pop();
|
2012-09-14 21:07:28 -03:00
|
|
|
if (b != -1) {
|
|
|
|
data[i] = (uint8_t) b;
|
|
|
|
} else {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
2012-09-14 14:58:55 -03:00
|
|
|
}
|
|
|
|
|
2012-12-06 15:02:28 -04:00
|
|
|
size_t AVRConsoleDriver::backend_write(const uint8_t *data, size_t len) {
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
2012-09-14 21:07:28 -03:00
|
|
|
bool valid = _rxbuf.push(data[i]);
|
|
|
|
if (!valid) {
|
|
|
|
return i;
|
|
|
|
}
|
2012-09-14 14:58:55 -03:00
|
|
|
}
|
2012-09-14 21:07:28 -03:00
|
|
|
return len;
|
2012-09-14 14:58:55 -03:00
|
|
|
}
|
|
|
|
|
2012-09-14 21:07:28 -03:00
|
|
|
// AVRConsoleDriver private method implementations ////////////////////////////
|
|
|
|
|
2012-09-14 14:58:55 -03:00
|
|
|
// BetterStream method implementations /////////////////////////////////////////
|
|
|
|
void AVRConsoleDriver::print_P(const prog_char_t *s) {
|
|
|
|
char c;
|
|
|
|
while ('\0' != (c = pgm_read_byte((const prog_char *)s++)))
|
|
|
|
write(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AVRConsoleDriver::println_P(const prog_char_t *s) {
|
|
|
|
print_P(s);
|
|
|
|
println();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AVRConsoleDriver::printf(const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vprintf((AP_HAL::Print*)this, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AVRConsoleDriver::_printf_P(const prog_char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vprintf((AP_HAL::Print*)this, 1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream method implementations /////////////////////////////////////////
|
2012-12-06 15:02:28 -04:00
|
|
|
int16_t AVRConsoleDriver::available(void) {
|
2012-09-14 14:58:55 -03:00
|
|
|
if (_user_backend) {
|
2012-09-14 21:30:39 -03:00
|
|
|
return _rxbuf.bytes_used();
|
2012-09-14 14:58:55 -03:00
|
|
|
} else {
|
|
|
|
return _base_uart->available();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 15:02:28 -04:00
|
|
|
int16_t AVRConsoleDriver::txspace(void) {
|
2012-09-14 14:58:55 -03:00
|
|
|
if (_user_backend) {
|
2012-09-14 21:30:39 -03:00
|
|
|
return _rxbuf.bytes_free();
|
2012-09-14 14:58:55 -03:00
|
|
|
} else {
|
|
|
|
return _base_uart->txspace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 15:02:28 -04:00
|
|
|
int16_t AVRConsoleDriver::read() {
|
2012-09-14 14:58:55 -03:00
|
|
|
if (_user_backend) {
|
2012-09-14 21:07:28 -03:00
|
|
|
return _rxbuf.pop();
|
2012-09-14 14:58:55 -03:00
|
|
|
} else {
|
|
|
|
return _base_uart->read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 15:02:28 -04:00
|
|
|
int16_t AVRConsoleDriver::peek() {
|
2012-09-14 14:58:55 -03:00
|
|
|
if (_user_backend) {
|
2012-09-14 21:30:39 -03:00
|
|
|
return _rxbuf.peek();
|
2012-09-14 14:58:55 -03:00
|
|
|
} else {
|
|
|
|
return _base_uart->peek();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 21:07:28 -03:00
|
|
|
// Print method implementations /////////////////////////////////////////
|
|
|
|
|
|
|
|
size_t AVRConsoleDriver::write(uint8_t c) {
|
|
|
|
if (_user_backend) {
|
|
|
|
return (_txbuf.push(c) ? 1 : 0);
|
|
|
|
} else {
|
|
|
|
return _base_uart->write(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AVRConsoleDriver::Buffer implementation.
|
|
|
|
* A synchronous nonblocking ring buffer, based on the AVRUARTDriver::Buffer
|
|
|
|
*/
|
|
|
|
|
2012-12-06 15:02:28 -04:00
|
|
|
bool AVRConsoleDriver::Buffer::allocate(uint16_t size) {
|
2012-09-14 21:07:28 -03:00
|
|
|
_head = 0;
|
|
|
|
_tail = 0;
|
|
|
|
uint8_t shift;
|
|
|
|
/* Hardcoded max size of 1024. sue me. */
|
|
|
|
for ( shift = 1;
|
|
|
|
( 1 << shift ) < 1024 && ( 1 << shift) < size;
|
|
|
|
shift++
|
|
|
|
) ;
|
|
|
|
uint16_t tmpmask = (1 << shift) - 1;
|
|
|
|
|
|
|
|
if ( _bytes != NULL ) {
|
|
|
|
if ( _mask == tmpmask ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
free(_bytes);
|
|
|
|
}
|
|
|
|
_mask = tmpmask;
|
|
|
|
_bytes = (uint8_t*) malloc(_mask+1);
|
|
|
|
return (_bytes != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AVRConsoleDriver::Buffer::push(uint8_t b) {
|
|
|
|
uint16_t next = (_head + 1) & _mask;
|
|
|
|
if ( next == _tail ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_bytes[_head] = b;
|
|
|
|
_head = next;
|
2012-10-26 21:02:45 -03:00
|
|
|
return true;
|
2012-09-14 21:07:28 -03:00
|
|
|
}
|
|
|
|
|
2012-12-06 15:02:28 -04:00
|
|
|
int16_t AVRConsoleDriver::Buffer::pop() {
|
2012-09-14 21:07:28 -03:00
|
|
|
if ( _tail == _head ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint8_t b = _bytes[_tail];
|
|
|
|
_tail = ( _tail + 1 ) & _mask;
|
2012-12-06 15:02:28 -04:00
|
|
|
return (int16_t) b;
|
2012-09-14 21:07:28 -03:00
|
|
|
}
|
|
|
|
|
2012-12-06 15:02:28 -04:00
|
|
|
int16_t AVRConsoleDriver::Buffer::peek() {
|
2012-09-14 21:30:39 -03:00
|
|
|
if ( _tail == _head ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint8_t b = _bytes[_tail];
|
2012-12-06 15:02:28 -04:00
|
|
|
return (int16_t) b;
|
2012-09-14 21:30:39 -03:00
|
|
|
}
|
|
|
|
|
2012-09-18 21:18:01 -03:00
|
|
|
uint16_t AVRConsoleDriver::Buffer::bytes_used() {
|
2012-09-14 21:30:39 -03:00
|
|
|
return ((_head - _tail) & _mask);
|
|
|
|
}
|
|
|
|
|
2012-09-18 21:18:01 -03:00
|
|
|
uint16_t AVRConsoleDriver::Buffer::bytes_free() {
|
2012-09-14 21:30:39 -03:00
|
|
|
return ((_mask+1) - ((_head - _tail) & _mask));
|
|
|
|
}
|
|
|
|
|