summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-05-05 18:35:39 +0200
committerNao Pross <naopross@thearcway.org>2018-05-05 18:35:39 +0200
commit7c6e90299de97b51f6ba17d1a50cedb824d0c82f (patch)
tree2a032bbec8a11e1b5f7a189ff2750936982576fc
parentFix split() (diff)
downloadSAMLiquidSmoke-7c6e90299de97b51f6ba17d1a50cedb824d0c82f.tar.gz
SAMLiquidSmoke-7c6e90299de97b51f6ba17d1a50cedb824d0c82f.zip
Add gpio::is_set() to read from _latch register, update command parsing
-rw-r--r--hal/pin.hpp12
-rw-r--r--hal/pin.tpp6
-rw-r--r--hal/uart.hpp5
-rw-r--r--hal/uart.tpp15
-rw-r--r--led.cpp8
-rw-r--r--led.hpp2
-rw-r--r--main.cpp42
7 files changed, 70 insertions, 20 deletions
diff --git a/hal/pin.hpp b/hal/pin.hpp
index bbb80dc..ab9d3ad 100644
--- a/hal/pin.hpp
+++ b/hal/pin.hpp
@@ -17,12 +17,19 @@ class gpio
public:
virtual ~gpio() {}
- // digital functions
+ // digital mode methods
virtual void set_mode(unsigned m) = 0;
+
+ // digital output methods
virtual void set(unsigned s) = 0;
- virtual unsigned read() const = 0;
+ virtual unsigned is_set() const = 0;
virtual void toggle() = 0;
+ // digital input methods
+ virtual unsigned read() const = 0;
+
+ // TODO: analog mode methods
+
protected:
gpio() {}
};
@@ -47,6 +54,7 @@ public:
void set_mode(mode m);
unsigned read() const;
+ unsigned is_set() const;
void set(unsigned s);
void set(state s);
diff --git a/hal/pin.tpp b/hal/pin.tpp
index 42d03de..527ba0a 100644
--- a/hal/pin.tpp
+++ b/hal/pin.tpp
@@ -52,6 +52,12 @@ unsigned io_pin<bit>::read() const
}
template<unsigned bit>
+unsigned io_pin<bit>::is_set() const
+{
+ return (*_latch & (1<<bit)) ? 1 : 0;
+}
+
+template<unsigned bit>
void io_pin<bit>::set(unsigned s)
{
if (s > 0)
diff --git a/hal/uart.hpp b/hal/uart.hpp
index b1dce29..65062f6 100644
--- a/hal/uart.hpp
+++ b/hal/uart.hpp
@@ -62,6 +62,11 @@ namespace uart
template<unsigned dev>
void initialize();
+#if 0
+ template<unsigned dev>
+ void set_baudrate(long baud);
+#endif
+
// the following functions have been inlined
// template<unsigned dev>
diff --git a/hal/uart.tpp b/hal/uart.tpp
index 0ecd615..359722e 100644
--- a/hal/uart.tpp
+++ b/hal/uart.tpp
@@ -193,8 +193,9 @@ io_pin<3> led_green(&LATBbits, &TRISBbits, &PORTBbits);
void __ISR(_UART_1_VECTOR, IPL1AUTO) usart_1_isr()
{
if (IFS1bits.U1RXIF) {
- // debug
+ #ifdef DEBUG
led_blue.toggle();
+ #endif
// wait for data to be available
while (!U1STAbits.URXDA);
@@ -213,10 +214,12 @@ void __ISR(_UART_1_VECTOR, IPL1AUTO) usart_1_isr()
}
// on each interrupt call, send one character
else if (IFS1bits.U1TXIF) {
+ #ifdef DEBUG
+ led_green.toggle();
+ #endif
// disable TX interrupt flag
IEC1bits.U1TXIE = 0;
- led_green.toggle();
// write data
U1TXREG = static_cast<decltype(U1TXREG)>(uart::tx_buffer<1>().front());
@@ -298,6 +301,14 @@ namespace uart
CFGCONbits.IOLOCK = 1; // lock PPS
hw::reglock();
}
+
+#if 0
+ template<>
+ void set_baudrate<1>(long baud)
+ {
+
+ }
+#endif
}
#endif \ No newline at end of file
diff --git a/led.cpp b/led.cpp
index 1d10e24..4888d84 100644
--- a/led.cpp
+++ b/led.cpp
@@ -9,7 +9,7 @@
led::led(gpio *pin, led::color color) : _pin(pin), _color(color)
{
- _pin->set_mode(0);
+ _pin->set_mode(0); // output
}
@@ -45,9 +45,9 @@ void led::set(unsigned s)
_pin->set(s);
}
-unsigned led::read() const
+unsigned led::is_set() const
{
- return _pin->read();
+ return _pin->is_set();
}
void led::toggle()
@@ -74,7 +74,7 @@ std::string led::to_string()
}
str += ") is ";
- str += (read()) ? "on" : "off";
+ str += (is_set()) ? "on" : "off";
return str;
}
diff --git a/led.hpp b/led.hpp
index 30b7a7a..0e674bb 100644
--- a/led.hpp
+++ b/led.hpp
@@ -29,7 +29,7 @@ public:
color set_color() const;
void set(unsigned s);
- unsigned read() const;
+ unsigned is_set() const;
void toggle();
std::string to_string();
diff --git a/main.cpp b/main.cpp
index 29263cd..46e5342 100644
--- a/main.cpp
+++ b/main.cpp
@@ -5,7 +5,7 @@
* Created on May 1, 2018, 6:18 PM
*/
-#define DEBUG
+//#define DEBUG
// basic devices
#include "hal/confbits.hpp"
@@ -86,19 +86,22 @@ int main(int argc, char** argv)
std::string input = "";
// write prompt
- uart::write<1>("> ");
+ uart::write<1>("\n\r> ");
do {
input += uart::read_wait<1>();
- } while (input.back() != '\r');
-
+ } while (input.back() != '\r' && input.back() != '\n');
+
#ifdef DEBUG
- uart::write<1>("\n\rinput: ");
+ uart::write<1>("input: ");
uart::print<1>(input);
#endif
// remove weird symbols
strip(input);
+
+ // set to lowercase
+ std::transform(input.begin(), input.end(), input.begin(), ::tolower);
// split
const std::vector<std::string> command = split(input, ' ');
@@ -112,19 +115,36 @@ int main(int argc, char** argv)
if (command.size() == 1) {
if (command[0] == "help") {
- uart::print<1>("TODO: help text");
+ uart::print<1>("List of available commands:");
+ uart::print<1>("help");
+ uart::print<1>("set led <color> <on/off>");
+ uart::print<1>("show led all <on/off>");
+ uart::print<1>("set baudrate <baud>");
+
+ uart::print<1>("\n\rList of available colors:");
+ uart::print<1>("red, green, yellow");
+
+ uart::print<1>("\n\rList of available baudrates:");
+ uart::print<1>("9600, 4800");
}
} else if (command.size() == 3) {
if (command[0] == "show") {
- uart::print<1>(led1.to_string());
- uart::print<1>(led2.to_string());
- uart::print<1>(led3.to_string());
- continue;
+ if (command[1] == "led") {
+ if (command[2] == "all") {
+ uart::print<1>(led1.to_string());
+ uart::print<1>(led2.to_string());
+ uart::print<1>(led3.to_string());
+ }
+ }
}
if (command[0] == "set") {
if (command[1] == "baudrate") {
- // TODO
+ if (command[2] == "9600") {
+ U1BRG = 0x19;
+ } else if (command[2] == "4800") {
+ U1BRG = 0x33;
+ }
}
}
} else if (command.size() == 4) {