/** EUSART Generated Driver File @Company Microchip Technology Inc. @File Name eusart.c @Summary This is the generated driver implementation file for the EUSART driver using PIC10 / PIC12 / PIC16 / PIC18 MCUs @Description This source file provides APIs for EUSART. Generation Information : Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.65.2 Device : PIC16F1579 Driver Version : 2.01 The generated drivers are tested against the following: Compiler : XC8 1.45 MPLAB : MPLAB X 4.15 */ /* (c) 2018 Microchip Technology Inc. and its subsidiaries. Subject to your compliance with these terms, you may use Microchip software and any derivatives exclusively with Microchip products. It is your responsibility to comply with third party license terms applicable to your use of third party software (including open source software) that may accompany Microchip software. THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. */ /** Section: Included Files */ #include "eusart.h" /** Section: Macro Declarations */ #define EUSART_TX_BUFFER_SIZE 16 #define EUSART_RX_BUFFER_SIZE 64 /** Section: Global Variables */ volatile uint8_t eusartTxHead = 0; volatile uint8_t eusartTxTail = 0; volatile uint8_t eusartTxBuffer[EUSART_TX_BUFFER_SIZE]; volatile uint8_t eusartTxBufferRemaining; volatile uint8_t eusartRxHead = 0; volatile uint8_t eusartRxTail = 0; volatile uint8_t eusartRxBuffer[EUSART_RX_BUFFER_SIZE]; volatile uint8_t eusartRxCount; /** Section: EUSART APIs */ void EUSART_Initialize(void) { // disable interrupts before changing states PIE1bits.RCIE = 0; EUSART_SetRxInterruptHandler(EUSART_Receive_ISR); PIE1bits.TXIE = 0; EUSART_SetTxInterruptHandler(EUSART_Transmit_ISR); // Set the EUSART module to the options selected in the user interface. // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled; BAUDCON = 0x08; // SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled; RCSTA = 0x90; // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave; TXSTA = 0x24; // SPBRGL 16; SPBRGL = 0x10; // SPBRGH 0; SPBRGH = 0x00; // initializing the driver state eusartTxHead = 0; eusartTxTail = 0; eusartTxBufferRemaining = sizeof(eusartTxBuffer); eusartRxHead = 0; eusartRxTail = 0; eusartRxCount = 0; // enable receive interrupt PIE1bits.RCIE = 1; } uint8_t EUSART_is_tx_ready(void) { return eusartTxBufferRemaining; } uint8_t EUSART_is_rx_ready(void) { return eusartRxCount; } bool EUSART_is_tx_done(void) { return TXSTAbits.TRMT; } uint8_t EUSART_Read(void) { uint8_t readValue = 0; while(0 == eusartRxCount) { } readValue = eusartRxBuffer[eusartRxTail++]; if(sizeof(eusartRxBuffer) <= eusartRxTail) { eusartRxTail = 0; } PIE1bits.RCIE = 0; eusartRxCount--; PIE1bits.RCIE = 1; return readValue; } void EUSART_Write(uint8_t txData) { while(0 == eusartTxBufferRemaining) { } if(0 == PIE1bits.TXIE) { TXREG = txData; } else { PIE1bits.TXIE = 0; eusartTxBuffer[eusartTxHead++] = txData; if(sizeof(eusartTxBuffer) <= eusartTxHead) { eusartTxHead = 0; } eusartTxBufferRemaining--; } PIE1bits.TXIE = 1; } void EUSART_Transmit_ISR(void) { // add your EUSART interrupt custom code if(sizeof(eusartTxBuffer) > eusartTxBufferRemaining) { TXREG = eusartTxBuffer[eusartTxTail++]; if(sizeof(eusartTxBuffer) <= eusartTxTail) { eusartTxTail = 0; } eusartTxBufferRemaining++; } else { PIE1bits.TXIE = 0; } } void EUSART_Receive_ISR(void) { if(1 == RCSTAbits.OERR) { // EUSART error - restart RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; } // buffer overruns are ignored eusartRxBuffer[eusartRxHead++] = RCREG; if(sizeof(eusartRxBuffer) <= eusartRxHead) { eusartRxHead = 0; } eusartRxCount++; } void EUSART_SetTxInterruptHandler(void (* interruptHandler)(void)){ EUSART_TxDefaultInterruptHandler = interruptHandler; } void EUSART_SetRxInterruptHandler(void (* interruptHandler)(void)){ EUSART_RxDefaultInterruptHandler = interruptHandler; } /** End of File */