Navigation Menu

Skip to content

Commit

Permalink
added async sending to async_usart (works with xmega only)
Browse files Browse the repository at this point in the history
fixed command_parser::send (clear m_tx_ptr after sending)
added command_parser::read
  • Loading branch information
cednik committed Jul 3, 2015
1 parent 321262d commit 00f5df8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
12 changes: 11 additions & 1 deletion async_usart.hpp
Expand Up @@ -4,6 +4,7 @@
#include <stdint.h>
#include "nobootseq.hpp"
#include "buffer.hpp"
#include "usart_base.hpp"

namespace avrlib {

Expand Down Expand Up @@ -82,8 +83,9 @@ class async_usart
this->process_tx();
sei();
}

m_tx_buffer.push(v);
if(m_async_tx)
m_usart.dre_interrupt(uart_intr_med);
}
else
{
Expand Down Expand Up @@ -153,6 +155,10 @@ class async_usart
m_tx_buffer.pop();
return true;
}
else
{
m_usart.dre_interrupt(uart_intr_off);
}
return false;
}

Expand All @@ -172,13 +178,17 @@ class async_usart

usart_type & usart() { return m_usart; }
usart_type const & usart() const { return m_usart; }

void async_tx(const bool& en) { m_async_tx = en; }
bool async_tx() const { return m_async_tx; }

private:
usart_type m_usart;
buffer<value_type, RxBufferSize> m_rx_buffer;
buffer<value_type, TxBufferSize==0?1:TxBufferSize> m_tx_buffer;
bootseq_type m_bootseq;
volatile overflow_type m_overflow;
volatile bool m_async_tx;
};

}
Expand Down
17 changes: 12 additions & 5 deletions command_parser.hpp
Expand Up @@ -81,12 +81,14 @@ class command_parser
return 255;
}

bool write(const uint8_t& v)
template <typename T>
bool write(const T& v)
{
if(m_tx_ptr == 15)
if(m_tx_ptr >= (16 - sizeof(v)))
return false;
m_tx_buffer[m_tx_ptr] = v;
++m_tx_ptr;
uint8_t const* begin = reinterpret_cast<uint8_t const*>(&v);
for(const uint8_t end = m_tx_ptr + sizeof(v); m_tx_ptr != end; ++m_tx_ptr, ++begin)
m_tx_buffer[m_tx_ptr] = *begin;
return true;
}

Expand All @@ -97,12 +99,16 @@ class command_parser
usart.write(((cmd & 0x0F)<<4) | m_tx_ptr);
for(uint8_t i = 0; i != m_tx_ptr; ++i)
usart.write(m_tx_buffer[i]);
m_tx_ptr = 0;
}

state_t state() const { return m_state; }

uint8_t operator[](uint8_t index) const { return m_rx_buffer[index]; }

template <typename T>
T read(const uint8_t& index) const { return *reinterpret_cast<T*>(m_tx_buffer + index); }

uint8_t* get_rx_buffer() { return m_rx_buffer; }
uint8_t* get_buffer() { return m_rx_buffer; }

Expand Down Expand Up @@ -210,6 +216,7 @@ class safe_command_parser
usart.write(m_tx_buffer[i]);
}
usart.write(chks);
m_tx_ptr = 0;
}

private:
Expand Down Expand Up @@ -241,7 +248,7 @@ class base_timed_command_parser

private:
Timer const & m_timer;
time_type /*const &*/ m_timeout;
time_type m_timeout;

time_type m_last_push;
};
Expand Down
23 changes: 23 additions & 0 deletions uart_xmega.hpp
Expand Up @@ -4,6 +4,8 @@
#include <avr/io.h>
#include <stdint.h>

#include "usart_base.hpp"

namespace avrlib {

enum uart_data_bits_t
Expand Down Expand Up @@ -133,6 +135,27 @@ class uart_xmega
return m_p;
}

void rxc_interrupt(const uart_interrupt_priority_t& priority)
{
m_p->CTRLA = (m_p->CTRLA & ~USART_RXCINTLVL_gm) | (priority<<USART_RXCINTLVL_gp);
}

bool rxc_interrupt() const { return (m_p->CTRLA & USART_RXCINTLVL_gm) != 0; }

void txc_interrupt(const uart_interrupt_priority_t& priority)
{
m_p->CTRLA = (m_p->CTRLA & ~USART_TXCINTLVL_gm) | (priority<<USART_TXCINTLVL_gp);
}

bool tx_interrupt() const { return (m_p->CTRLA & USART_TXCINTLVL_gm) != 0; }

void dre_interrupt(const uart_interrupt_priority_t& priority)
{
m_p->CTRLA = (m_p->CTRLA & ~USART_DREINTLVL_gm) | (priority<<USART_DREINTLVL_gp);
}

bool dre_interrupt() const { return (m_p->CTRLA & USART_DREINTLVL_gm) != 0; }

private:
USART_t * m_p;
};
Expand Down
8 changes: 8 additions & 0 deletions usart_base.hpp
Expand Up @@ -9,6 +9,14 @@ struct ubrr
static uint16_t const value = ((F_CPU / (4 * speed) + 1) >> 1) - 1;
};

enum uart_interrupt_priority_t
{
uart_intr_off = 0,
uart_intr_lo = 1,
uart_intr_med = 2,
uart_intr_hi = 3
};

namespace detail {

inline uint16_t get_ubrr(uint32_t speed)
Expand Down

0 comments on commit 00f5df8

Please sign in to comment.