From 7bec778c9a59416e3214ac28a6bce29dd03c9c00 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Fri, 4 May 2018 01:37:04 +0200 Subject: Update pin class to support input and output mode Also, minor changes in uart1.tpp --- hal/pin.hpp | 31 ++++++++++++-- hal/pin.tpp | 78 ++++++++++++++++++++++++++++++----- hal/uart1.tpp | 5 +++ main.cpp | 9 ++-- nbproject/Makefile-genesis.properties | 4 +- nbproject/configurations.xml | 10 ++++- nbproject/project.xml | 2 +- 7 files changed, 117 insertions(+), 22 deletions(-) diff --git a/hal/pin.hpp b/hal/pin.hpp index 9df22d4..6625f90 100644 --- a/hal/pin.hpp +++ b/hal/pin.hpp @@ -8,16 +8,39 @@ #ifndef PIN_HPP #define PIN_HPP -template +template class pin { public: - pin(reg *r); + enum class mode : unsigned + { INPUT = 1, OUTPUT = 0 }; + + enum class state : unsigned + { ON = 1, OFF = 0 }; + + pin() = delete; + + template + pin(latch_T *latch, tris_T *tris, port_T *port); + virtual ~pin(); - void set(bool v); + void set_mode(unsigned m); + void set_mode(mode m); + + state read() const; + + void set(unsigned s); + void set(state s); + + void toggle(); + + bool operator==(const pin &other) const; + bool operator!=(const pin &other) const; private: - reg *_register; + volatile uint8_t *_latch; + volatile uint8_t *_tris; + volatile uint8_t *_port; }; #endif /* PIN_HPP */ \ No newline at end of file diff --git a/hal/pin.tpp b/hal/pin.tpp index 4f6bd4a..c3c005f 100644 --- a/hal/pin.tpp +++ b/hal/pin.tpp @@ -7,24 +7,82 @@ #include "pin.hpp" -template -pin::pin(reg *r) + +template +template +pin::pin(latch_T *latch, tris_T *tris, port_T *port) : + _latch(reinterpret_cast(latch)), + _tris(reinterpret_cast(tris)), + _port(reinterpret_cast(port)) { - _register = r; + // default settings + set_mode(pin::mode::OUTPUT); + set(pin::state::OFF); } -template -pin::~pin() +template +pin::~pin() { } -template -void pin::set(bool v) + +template +void pin::set_mode(unsigned m) +{ + if (m) + *_tris |= 1< +void pin::set_mode(pin::mode m) +{ + set(static_cast(m)); +} + +template +typename pin::state pin::read() const +{ + if (*_tris & (1< +void pin::set(unsigned s) { - if (v) - *reinterpret_cast(_register) |= 1<(_register) &= ~(1< +void pin::set(pin::state s) +{ + set(static_cast(s)); +} + +template +void pin::toggle() +{ + *_latch ^= 1< +bool pin::operator==(const pin &other) const +{ + return (_latch == other._latch + && _tris == other._tris + && _port == other._port); +} + +template +bool pin::operator!=(const pin &other) const +{ + return !(*this == other); } \ No newline at end of file diff --git a/hal/uart1.tpp b/hal/uart1.tpp index a8a32f0..9526650 100644 --- a/hal/uart1.tpp +++ b/hal/uart1.tpp @@ -17,6 +17,11 @@ void __ISR(_UART_1_VECTOR, IPL1AUTO) usart_1_isr() { if (IFS1bits.U1RXIF) { uart::rx_buffer[0].push_back(static_cast(U1RXREG)); + + IFS1bits.U1RXIF = 0; + } else if (IFS1bits.U1TXIF) { + + IFS1bits.U1TXIF = 0; } } diff --git a/main.cpp b/main.cpp index 5347507..59a9097 100644 --- a/main.cpp +++ b/main.cpp @@ -21,12 +21,13 @@ extern "C" { int main(int argc, char** argv) { - LATE = 0x0000; - TRISE = 0x002f; - CNPUE = 0x0000; + pin<4> led1(&LATEbits, &TRISEbits, &PORTEbits); + pin<6> led2(&LATEbits, &TRISEbits, &PORTEbits); + pin<7> led3(&LATEbits, &TRISEbits, &PORTEbits); - pin led1(&LATEbits); led1.set(1); + led2.set(1); + led3.set(1); while (true) { } diff --git a/nbproject/Makefile-genesis.properties b/nbproject/Makefile-genesis.properties index 15458e7..48b997c 100644 --- a/nbproject/Makefile-genesis.properties +++ b/nbproject/Makefile-genesis.properties @@ -1,8 +1,8 @@ # -#Fri May 04 00:12:50 CEST 2018 +#Fri May 04 00:51:30 CEST 2018 default.com-microchip-mplab-nbide-toolchainXC32-XC32LanguageToolchain.md5=fcf9db1a3d46b4ef4e0a46afcbf02251 default.languagetoolchain.dir=/opt/microchip/xc32/v2.05/bin -configurations-xml=7da60577239442fa2d5644589c71b844 +configurations-xml=662763a5f521e2ac61177edbdd1d9879 com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=c8c2915d32f5d7e4be49831bc9827ab0 default.languagetoolchain.version=2.05 host.platform=linux diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index ced59db..c64a84e 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -4,6 +4,10 @@ + + hal/pin.hpp + hal/uart.hpp + @@ -20,6 +24,10 @@ + + hal/pin.tpp + hal/uart1.tpp + @@ -39,6 +47,7 @@ mcc_generated_files + hal Makefile @@ -211,7 +220,6 @@ - diff --git a/nbproject/project.xml b/nbproject/project.xml index 2cba338..49672e7 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -8,7 +8,7 @@ 0 c cpp - h + h,hpp ISO-8859-1 -- cgit v1.2.1