195 lines
3.5 KiB
C++
195 lines
3.5 KiB
C++
#include "clock.h"
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "flash/flash.hpp"
|
|
#include "io/io.hpp"
|
|
#include "uart/uart.h"
|
|
#include "uart/uart.hpp"
|
|
#include "uart/usart.h"
|
|
|
|
void newUartUsage()
|
|
{
|
|
using namespace uart;
|
|
Uart0<> serial;
|
|
Uart1<> serial1;
|
|
|
|
serial << "Hello World from RAM. " << F("Hello World from flash\r\n");
|
|
serial1 << "Hello World from RAM. " << F("Hello World from flash\r\n");
|
|
|
|
_delay_ms(1000);
|
|
}
|
|
|
|
void newUartUsage2()
|
|
{
|
|
using namespace uart;
|
|
|
|
using config = Config<9600, DataBits::EIGHT, Parity::NONE, StopBits::ONE>;
|
|
using uart0 = Hardware0<Mode::ASYNCHRONOUS, config, Driven::INTERRUPT>;
|
|
using softuart = Software<io::P::B1, io::P::B2, config>;
|
|
|
|
Uart<uart0> serial;
|
|
Uart<softuart> softSerial;
|
|
|
|
serial << "Hello World from RAM. " << F("Hello World from flash\r\n");
|
|
// softSerial << "Hello World using finalized software interface!" << F("\r\nAlso greetz from progmem\r\n");
|
|
|
|
_delay_ms(1000);
|
|
}
|
|
|
|
void oldUsartUsage()
|
|
{
|
|
USART0 &serial = USART0::inst();
|
|
serial.init(9600);
|
|
|
|
serial << "Hello World from RAM. "
|
|
<< "Hello World from flash\r\n";
|
|
|
|
_delay_ms(1000);
|
|
}
|
|
|
|
namespace spi {
|
|
|
|
enum class Cpol {
|
|
MODE_0,
|
|
MODE_1,
|
|
};
|
|
|
|
enum class Cpha {
|
|
MODE_0,
|
|
MODE_1,
|
|
};
|
|
|
|
enum class DataOrder {
|
|
MSB,
|
|
LSB,
|
|
};
|
|
|
|
template <Cpol cpol, Cpha cpha, DataOrder dataOrder>
|
|
struct Config {
|
|
static constexpr auto CPOL_MODE = cpol;
|
|
static constexpr auto CPHA_MODE = cpha;
|
|
static constexpr auto DATA_ORDER = dataOrder;
|
|
};
|
|
|
|
template <class Driver>
|
|
struct spi {
|
|
spi()
|
|
{
|
|
Driver::init();
|
|
}
|
|
};
|
|
|
|
} // namespace spi
|
|
|
|
namespace uart {
|
|
|
|
template <class Config>
|
|
class Hardware0<Mode::SPI, Config> {
|
|
public:
|
|
static void init()
|
|
{
|
|
UCSR0C |= (1 << UMSEL01) | (1 << UMSEL00);
|
|
|
|
if (DATA_ORDER == spi::DataOrder::MSB)
|
|
UCSR0C &= ~(1 << UCSZ01);
|
|
else
|
|
UCSR0C |= (1 << UCSZ01);
|
|
|
|
if (CPOL_MODE == spi::Cpol::MODE_0)
|
|
UCSR0C &= ~(1 << UCPOL0);
|
|
else
|
|
UCSR0C |= (1 << UCPOL0);
|
|
|
|
if (CPHA_MODE == spi::Cpha::MODE_0)
|
|
UCSR0C &= ~(1 << UCSZ00);
|
|
else
|
|
UCSR0C |= (1 << UCSZ00);
|
|
}
|
|
|
|
private:
|
|
static constexpr auto CPOL_MODE = Config::CPOL_MODE;
|
|
static constexpr auto CPHA_MODE = Config::CPHA_MODE;
|
|
static constexpr auto DATA_ORDER = Config::DATA_ORDER;
|
|
};
|
|
|
|
} // namespace uart
|
|
|
|
void spiTest()
|
|
{
|
|
using config = spi::Config<spi::Cpol::MODE_0, spi::Cpha::MODE_0, spi::DataOrder::MSB>;
|
|
using uartspi = uart::Hardware0<uart::Mode::SPI, config>;
|
|
|
|
spi::spi<uartspi> uartSpi;
|
|
}
|
|
|
|
static inline void initUart(const uint32_t baudRate)
|
|
{
|
|
UBRR0 = static_cast<uint16_t>((F_CPU / (16 * baudRate)) - 1);
|
|
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
|
|
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
|
|
}
|
|
|
|
static inline void txUart(uint8_t byte)
|
|
{
|
|
while (!(UCSR0A & (1 << UDRE0)))
|
|
;
|
|
|
|
UDR0 = byte;
|
|
}
|
|
|
|
static inline void txString(const char *str)
|
|
{
|
|
while (char ch = *str++)
|
|
txUart(ch);
|
|
}
|
|
|
|
static inline void txString(const detail::FlashString *str)
|
|
{
|
|
const char *strIt = reinterpret_cast<const char *>(str);
|
|
|
|
while (char ch = pgm_read_byte(strIt++))
|
|
txUart(ch);
|
|
}
|
|
|
|
void optimalUartTest()
|
|
{
|
|
auto ramString = "Hello World from RAM. ";
|
|
auto flashString = F("Hello World from flash\r\n");
|
|
|
|
initUart(9600);
|
|
|
|
txString(ramString);
|
|
txString(flashString);
|
|
|
|
_delay_ms(1000);
|
|
}
|
|
|
|
void cUartLibTest()
|
|
{
|
|
auto ramString = "Hello World from RAM. ";
|
|
auto flashString = F("Hello World from flash\r\n");
|
|
|
|
uart_init(UART_BAUD_SELECT(9600, F_CPU));
|
|
sei();
|
|
|
|
uart_puts(ramString);
|
|
uart_puts_p(reinterpret_cast<const char *>(flashString));
|
|
|
|
_delay_ms(1000);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
newUartUsage();
|
|
// oldUsartUsage();
|
|
// optimalUartTest();
|
|
// cUartLibTest();
|
|
|
|
// spiTest();
|
|
|
|
return 0;
|
|
}
|