summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--hal/pin.hpp31
-rw-r--r--hal/pin.tpp78
-rw-r--r--hal/uart1.tpp5
-rw-r--r--main.cpp9
-rw-r--r--nbproject/Makefile-genesis.properties4
-rw-r--r--nbproject/configurations.xml10
-rw-r--r--nbproject/project.xml2
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<typename reg, unsigned bit>
+template<unsigned bit>
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<typename latch_T, typename tris_T, typename port_T>
+ 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<bit> &other) const;
+ bool operator!=(const pin<bit> &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<typename reg, unsigned bit>
-pin<reg, bit>::pin(reg *r)
+
+template<unsigned bit>
+template<typename latch_T, typename tris_T, typename port_T>
+pin<bit>::pin(latch_T *latch, tris_T *tris, port_T *port) :
+ _latch(reinterpret_cast<volatile uint8_t *>(latch)),
+ _tris(reinterpret_cast<volatile uint8_t *>(tris)),
+ _port(reinterpret_cast<volatile uint8_t *>(port))
{
- _register = r;
+ // default settings
+ set_mode(pin<bit>::mode::OUTPUT);
+ set(pin<bit>::state::OFF);
}
-template<typename reg, unsigned bit>
-pin<reg, bit>::~pin()
+template<unsigned bit>
+pin<bit>::~pin()
{
}
-template<typename reg, unsigned bit>
-void pin<reg, bit>::set(bool v)
+
+template<unsigned bit>
+void pin<bit>::set_mode(unsigned m)
+{
+ if (m)
+ *_tris |= 1<<bit;
+ else
+ *_tris &= ~(1<<bit);
+}
+
+template<unsigned bit>
+void pin<bit>::set_mode(pin<bit>::mode m)
+{
+ set(static_cast<unsigned>(m));
+}
+
+template<unsigned bit>
+typename pin<bit>::state pin<bit>::read() const
+{
+ if (*_tris & (1<<bit))
+ return state::ON;
+ else
+ return state::OFF;
+}
+
+template<unsigned bit>
+void pin<bit>::set(unsigned s)
{
- if (v)
- *reinterpret_cast<volatile uint8_t *>(_register) |= 1<<bit;
+ if (s)
+ *_latch |= 1<<bit;
else
- *reinterpret_cast<volatile uint8_t *>(_register) &= ~(1<<bit);
+ *_latch &= ~(1<<bit);
+}
+
+template<unsigned bit>
+void pin<bit>::set(pin<bit>::state s)
+{
+ set(static_cast<unsigned>(s));
+}
+
+template<unsigned bit>
+void pin<bit>::toggle()
+{
+ *_latch ^= 1<<bit;
+}
+
+template<unsigned bit>
+bool pin<bit>::operator==(const pin<bit> &other) const
+{
+ return (_latch == other._latch
+ && _tris == other._tris
+ && _port == other._port);
+}
+
+template<unsigned bit>
+bool pin<bit>::operator!=(const pin<bit> &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<uint8_t>(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<decltype(LATEbits), 4> 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 @@
<logicalFolder name="HeaderFiles"
displayName="Header Files"
projectFiles="true">
+ <logicalFolder name="f1" displayName="hal" projectFiles="true">
+ <itemPath>hal/pin.hpp</itemPath>
+ <itemPath>hal/uart.hpp</itemPath>
+ </logicalFolder>
<logicalFolder name="MCC Generated Files"
displayName="MCC Generated Files"
projectFiles="true">
@@ -20,6 +24,10 @@
<logicalFolder name="SourceFiles"
displayName="Source Files"
projectFiles="true">
+ <logicalFolder name="hal" displayName="hal" projectFiles="true">
+ <itemPath>hal/pin.tpp</itemPath>
+ <itemPath>hal/uart1.tpp</itemPath>
+ </logicalFolder>
<logicalFolder name="MCC Generated Files"
displayName="MCC Generated Files"
projectFiles="true">
@@ -39,6 +47,7 @@
</logicalFolder>
<sourceRootList>
<Elem>mcc_generated_files</Elem>
+ <Elem>hal</Elem>
</sourceRootList>
<projectmakefile>Makefile</projectmakefile>
<confs>
@@ -211,7 +220,6 @@
<property key="ToolFirmwareFilePath"
value="Press to browse for a specific firmware version"/>
<property key="ToolFirmwareOption.UseLatestFirmware" value="true"/>
- <property key="firmware.download.all" value="false"/>
<property key="memories.configurationmemory" value="true"/>
<property key="memories.dataflash" value="true"/>
<property key="memories.eeprom" value="true"/>
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 @@
<make-project-type>0</make-project-type>
<c-extensions>c</c-extensions>
<cpp-extensions>cpp</cpp-extensions>
- <header-extensions>h</header-extensions>
+ <header-extensions>h,hpp</header-extensions>
<asminc-extensions/>
<sourceEncoding>ISO-8859-1</sourceEncoding>
<make-dep-projects/>