diff options
Diffstat (limited to 'hal')
-rw-r--r-- | hal/pin.hpp | 23 | ||||
-rw-r--r-- | hal/pin.tpp | 30 | ||||
-rw-r--r-- | hal/uart.hpp | 87 | ||||
-rw-r--r-- | hal/uart1.tpp | 45 |
4 files changed, 185 insertions, 0 deletions
diff --git a/hal/pin.hpp b/hal/pin.hpp new file mode 100644 index 0000000..9df22d4 --- /dev/null +++ b/hal/pin.hpp @@ -0,0 +1,23 @@ +/* + * File: pin.hpp + * Author: naopross + * + * Created on May 3, 2018, 8:02 PM + */ + +#ifndef PIN_HPP +#define PIN_HPP + +template<typename reg, unsigned bit> +class pin { +public: + pin(reg *r); + virtual ~pin(); + + void set(bool v); + +private: + reg *_register; +}; + +#endif /* PIN_HPP */
\ No newline at end of file diff --git a/hal/pin.tpp b/hal/pin.tpp new file mode 100644 index 0000000..4f6bd4a --- /dev/null +++ b/hal/pin.tpp @@ -0,0 +1,30 @@ +/* + * File: pin.cpp + * Author: naopross + * + * Created on May 3, 2018, 8:02 PM + */ + +#include "pin.hpp" + +template<typename reg, unsigned bit> +pin<reg, bit>::pin(reg *r) +{ + _register = r; +} + + +template<typename reg, unsigned bit> +pin<reg, bit>::~pin() +{ + +} + +template<typename reg, unsigned bit> +void pin<reg, bit>::set(bool v) +{ + if (v) + *reinterpret_cast<volatile uint8_t *>(_register) |= 1<<bit; + else + *reinterpret_cast<volatile uint8_t *>(_register) &= ~(1<<bit); +}
\ No newline at end of file diff --git a/hal/uart.hpp b/hal/uart.hpp new file mode 100644 index 0000000..179dd4a --- /dev/null +++ b/hal/uart.hpp @@ -0,0 +1,87 @@ +/* + * File: uart.hpp + * Author: naopross + * + * Created on May 2, 2018, 7:04 PM + */ + +#ifndef UART_HPP +#define UART_HPP + +#include <cstdint> +#include <cstddef> +#include <string> + +extern "C" { +void usart_1_isr(); +void usart_2_isr(); +void usart_3_isr(); +void usart_4_isr(); +} + +namespace uart +{ + const unsigned devices_count = 4; + + enum class status : unsigned int + { + rx_data_available = 1<<0, + rx_overrun_error = 1<<1, + framing_error = 1<<2, + parity_error = 1<<3, + receiver_ide = 1<<4, + tx_complete = 1<<8, + tx_full = 1<<9, + }; + + enum class transfer_status : unsigned int + { + rx_full = 1<<0, + rx_data_present = 1<<1, + rx_empty = 1<<2, + tx_full = 1<<3, + tx_empty = 1<<4, + }; + + std::string rx_buffer[devices_count]; + std::string tx_buffer[devices_count]; + + template<unsigned dev> + void initialize(); + + template<unsigned dev> + uint8_t peek(uint16_t offset); + + template<unsigned dev> + uint8_t read(void); + + template<unsigned dev> + unsigned read(uint8_t *buffer, const unsigned numbytes); + + template<unsigned dev> + void write(const uint8_t byte); + + template<unsigned dev> + unsigned write(const uint8_t *buffer, const unsigned numbytes); + + template<unsigned dev> + status status(); + + template<unsigned dev> + transfer_status tranfer_status(); + + template<unsigned dev> + unsigned rx_buffer_size(); + + template<unsigned dev> + unsigned tx_buffer_size(); + + template<unsigned dev> + bool rx_buffer_empty(); + + template<unsigned dev> + bool tx_buffer_full(); +} + +#endif /* UART_HPP */ + diff --git a/hal/uart1.tpp b/hal/uart1.tpp new file mode 100644 index 0000000..a8a32f0 --- /dev/null +++ b/hal/uart1.tpp @@ -0,0 +1,45 @@ +/* + * File: uart.cpp + * Author: naopross + * + * Created on May 2, 2018, 7:05 PM + */ + +#include "uart.hpp" + +extern "C" { +#include <proc/p32mx470f512h.h> +#include <sys/attribs.h> +} + + +void __ISR(_UART_1_VECTOR, IPL1AUTO) usart_1_isr() +{ + if (IFS1bits.U1RXIF) { + uart::rx_buffer[0].push_back(static_cast<uint8_t>(U1RXREG)); + } +} + +namespace uart +{ + template<> + void initialize<1>() + { + // STSEL 1S; IREN disabled; PDSEL 8N; RTSMD disabled; RXINV disabled; SIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH enabled; UEN TX_RX; ON enabled; + U1MODE = 0x8008; + // UTXISEL TX_ONE_CHAR; UTXINV disabled; ADDR 0; URXEN disabled; OERR disabled; ADM_EN disabled; URXISEL RX_ONE_CHAR; UTXBRK disabled; UTXEN disabled; ADDEN disabled; + U1STA = 0x0; + // U1TXREG 0; + U1TXREG = 0x0; + // BaudRate = 9600; Frequency = 1000000 Hz; BRG 25; + U1BRG = 0x19; + + IEC1bits.U1RXIE = 1; + + U1STAbits.UTXEN = 1; + U1STAbits.URXEN = 1; + + //Enabling UART + U1MODEbits.ON = 1; + } +}
\ No newline at end of file |