summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-05-04 20:47:39 +0200
committerNao Pross <naopross@thearcway.org>2018-05-04 20:47:39 +0200
commite82789253b7c915270cfdc126e8e000924d90444 (patch)
treea58d4194d22e507afe506159b12c0cb2d182be7f
parentImplement uart TX features (diff)
downloadSAMLiquidSmoke-e82789253b7c915270cfdc126e8e000924d90444.tar.gz
SAMLiquidSmoke-e82789253b7c915270cfdc126e8e000924d90444.zip
Implement TX read(n) and readline() (not working yet)
-rw-r--r--hal/hwconfig.hpp8
-rw-r--r--hal/uart.hpp16
-rw-r--r--hal/uart.tpp52
-rw-r--r--main.cpp12
4 files changed, 75 insertions, 13 deletions
diff --git a/hal/hwconfig.hpp b/hal/hwconfig.hpp
index 0cd38ac..2195b8e 100644
--- a/hal/hwconfig.hpp
+++ b/hal/hwconfig.hpp
@@ -5,18 +5,18 @@
namespace hw
{
- void reglock();
- void regunlock();
+ void reglock();
+ void regunlock();
}
namespace osc
{
- void initialize();
+ void initialize();
}
namespace interrupts
{
- void initialize();
+ void initialize();
}
#endif \ No newline at end of file
diff --git a/hal/uart.hpp b/hal/uart.hpp
index 0be5835..60dd5bb 100644
--- a/hal/uart.hpp
+++ b/hal/uart.hpp
@@ -13,10 +13,10 @@
#include <queue>
extern "C" {
-void usart_1_isr();
-void usart_2_isr();
-void usart_3_isr();
-void usart_4_isr();
+ void usart_1_isr();
+ void usart_2_isr();
+ void usart_3_isr();
+ void usart_4_isr();
}
namespace uart
@@ -52,8 +52,16 @@ namespace uart
template<unsigned dev>
uint8_t read();
+#if 0
+ template<unsigned dev>
+ std::string read(const unsigned len);
+
+ template<unsigned dev>
+ std::string readline(const std::string &eol = "\n\r");
+
template<unsigned dev>
unsigned read(uint8_t *buffer, const unsigned numbytes);
+#endif
template<unsigned dev>
void write(const uint8_t byte);
diff --git a/hal/uart.tpp b/hal/uart.tpp
index e4b722e..148ff7d 100644
--- a/hal/uart.tpp
+++ b/hal/uart.tpp
@@ -25,18 +25,23 @@ extern "C" {
/* templated functions */
namespace uart
{
+ // access buffers
template<unsigned dev>
inline std::queue<uint8_t>& rx_buffer()
{
+ static_assert(dev <= devices_count, "invalid device number");
return uart::_rx_buffer[dev -1];
}
template<unsigned dev>
inline std::queue<uint8_t>& tx_buffer()
{
+ static_assert(dev <= devices_count, "invalid device number");
return uart::_tx_buffer[dev -1];
}
+
+ // read from serial device
template<unsigned dev>
uint8_t read()
{
@@ -46,6 +51,47 @@ namespace uart
return byte;
}
+#if 0
+ template<unsigned dev>
+ std::string read(const unsigned len)
+ {
+ std::string str = "";
+ unsigned i = len;
+
+ if (rx_buffer<dev>().size() < len) {
+ i = rx_buffer<dev>().size();
+ }
+
+ while (i--) {
+ str += read<dev>();
+ }
+
+ return str;
+ }
+
+ template<unsigned dev>
+ std::string readline(const std::string& eol = "\n\r")
+ {
+ int eol_c = 0;
+
+ std::string str = "";
+ unsigned i = rx_buffer<dev>().size();
+
+ while (i--) {
+ str += read<dev>();
+
+ // compare string endings
+ // TODO: make it more efficient
+ if (eol_c == eol.size()) {
+ break;
+ }
+
+ if (str.back() == eol[eol_c]) {
+ eol_c++;
+ }
+ }
+ }
+
template<unsigned dev>
unsigned read(uint8_t *buffer, const unsigned numbytes)
{
@@ -56,7 +102,10 @@ namespace uart
*(bptr++) = read<dev>();
}
}
+#endif
+
+ // write to serial device
template<unsigned dev>
void write(const uint8_t byte)
{
@@ -94,10 +143,11 @@ namespace uart
/* specializations for UART1 */
-// debug
+#ifdef DEBUG
#include "pin.tpp"
pin<2> led_blue(&LATBbits, &TRISBbits, &PORTBbits);
pin<3> led_green(&LATBbits, &TRISBbits, &PORTBbits);
+#endif
void __ISR(_UART_1_VECTOR, IPL1AUTO) usart_1_isr()
{
diff --git a/main.cpp b/main.cpp
index d95bc74..860460c 100644
--- a/main.cpp
+++ b/main.cpp
@@ -5,7 +5,7 @@
* Created on May 1, 2018, 6:18 PM
*/
-#include <string>
+#define DEBUG
// basic devices
#include "hal/confbits.hpp"
@@ -15,6 +15,10 @@
#include "hal/uart.tpp"
#include "hal/pin.tpp"
+// standard library
+#include <string>
+
+// microchip libraries
extern "C" {
#include <xc.h>
// #include <proc/p32mx470f512h.h>
@@ -40,9 +44,9 @@ int main(int argc, char** argv)
while (true) {
while (uart::rx_buffer<1>().empty());
-
- while (!uart::rx_buffer<1>().empty()) {
- uint8_t c = uart::read<1>();
+ while (uart::rx_buffer<1>().size() > 0) {
+ char c = uart::read<1>();
+
uart::write<1>(c);
uart::write<1>("\n\r");
}