summaryrefslogtreecommitdiffstats
path: root/hal/uart.tpp
diff options
context:
space:
mode:
Diffstat (limited to 'hal/uart.tpp')
-rw-r--r--hal/uart.tpp102
1 files changed, 84 insertions, 18 deletions
diff --git a/hal/uart.tpp b/hal/uart.tpp
index 44d9315..8c120ec 100644
--- a/hal/uart.tpp
+++ b/hal/uart.tpp
@@ -1,14 +1,23 @@
/*
- * File: uart.cpp
+ * File: uart.tpp
* Author: naopross
*
* Created on May 2, 2018, 7:05 PM
+ *
+ * Note: Templated functions cannot be created outside of a namespace definition
+ * because of a g++ bug in version 4.8.0 on which xc++ is based.
+ * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
*/
+#ifndef UART_TPP
+#define UART_TPP
+
+#include "hwconfig.hpp"
#include "uart.hpp"
extern "C" {
-#include <proc/p32mx470f512h.h>
+// #include <proc/p32mx470f512h.h>
+#include <xc.h>
#include <sys/attribs.h>
}
@@ -17,36 +26,81 @@ extern "C" {
namespace uart
{
template<unsigned dev>
- uint8_t peek<dev>(uint16_t offset)
+ bool rx_buffer_empty()
+ {
+ return rx_buffer[dev -1].empty();
+ }
+
+ template<unsigned dev>
+ bool tx_buffer_full()
{
- if (offset >= rx_buffer[dev -1].size())
- return 0;
- else
- return rx_buffer[dev -1].at(offset);
+ return !tx_buffer[dev -1].empty();
}
template<unsigned dev>
- bool rx_buffer_empty<dev>()
+ uint8_t read()
{
- return rx_buffer[0].empty();
+ uint8_t byte = rx_buffer[dev -1].front();
+ rx_buffer[dev -1].pop();
+
+ return byte;
+ }
+
+ template<unsigned dev>
+ unsigned read(uint8_t *buffer, const unsigned numbytes)
+ {
+ uint8_t *bptr = buffer;
+ unsigned len = numbytes;
+
+ while (len--) {
+ *(bptr++) = read<dev>();
+ }
+ }
+
+ template<unsigned dev>
+ void write(const uint8_t byte)
+ {
+
}
template<unsigned dev>
- bool tx_buffer_full<dev>()
+ void write(const std::string &str)
{
- return !tx_buffer[0].empty();
+ for (const char &c : str) {
+ write<dev>(c);
+ }
+ }
+
+ template<unsigned dev>
+ unsigned write(const uint8_t *buffer, const unsigned numbytes)
+ {
+
}
}
-/* specialization for UART1 */
+/* specializations for UART1 */
+
+// debug
+#include "pin.tpp"
+pin<2> led_blue(&LATBbits, &TRISBbits, &PORTEbits);
+
void __ISR(_UART_1_VECTOR, IPL1AUTO) usart_1_isr()
{
if (IFS1bits.U1RXIF) {
- uart::rx_buffer[0].push_back(static_cast<uint8_t>(U1RXREG));
+ // debug
+ led_blue.toggle();
+ while (U1STAbits.URXDA) {
+ uart::rx_buffer[0].push(static_cast<uint8_t>(U1RXREG));
+ }
IFS1bits.U1RXIF = 0;
} else if (IFS1bits.U1TXIF) {
+ IFS1bits.U1TXIF = 0;
+ } else {
+ if (U1STAbits.OERR == 1) {
+ U1STAbits.OERR = 0;
+ }
IFS1bits.U1TXIF = 0;
}
@@ -93,13 +147,25 @@ namespace uart
U1STAbits.UTXEN = 1;
U1STAbits.URXEN = 1;
- //Enabling UART
+ // Enabling UART
U1MODEbits.ON = 1;
- }
- template<>
- uint8_t read<1>()
- {
+ // UERI: UART1 Error
+ // Priority: 1
+ // SubPriority: 0
+ IPC7bits.U1IP = 1;
+ IPC7bits.U1IS = 0;
+ // map uart pins to port F
+ hw::regunlock(); // unlock PPS
+ CFGCONbits.IOLOCK = 0;
+
+ U1RXRbits.U1RXR = 0x0004; //RF1->UART1:U1RX;
+ RPF0Rbits.RPF0R = 0x0003; //RF0->UART1:U1TX;
+
+ CFGCONbits.IOLOCK = 1; // lock PPS
+ hw::reglock();
}
}
+
+#endif \ No newline at end of file