/* * 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 #include #include } /* templated functions */ namespace uart { template bool rx_buffer_empty() { return rx_buffer[dev -1].empty(); } template bool tx_buffer_full() { return !tx_buffer[dev -1].empty(); } template uint8_t read() { uint8_t byte = rx_buffer[dev -1].front(); rx_buffer[dev -1].pop(); return byte; } template unsigned read(uint8_t *buffer, const unsigned numbytes) { uint8_t *bptr = buffer; unsigned len = numbytes; while (len--) { *(bptr++) = read(); } } template void write(const uint8_t byte) { } template void write(const std::string &str) { for (const char &c : str) { write(c); } } template unsigned write(const uint8_t *buffer, const unsigned numbytes) { } } /* 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) { // debug led_blue.toggle(); while (U1STAbits.URXDA) { uart::rx_buffer[0].push(static_cast(U1RXREG)); } IFS1bits.U1RXIF = 0; } else if (IFS1bits.U1TXIF) { IFS1bits.U1TXIF = 0; } else { if (U1STAbits.OERR == 1) { U1STAbits.OERR = 0; } IFS1bits.U1TXIF = 0; } } 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; // 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