240 lines
6.0 KiB
C
240 lines
6.0 KiB
C
/**
|
|
EUSART1 Generated Driver File
|
|
|
|
@Company
|
|
Microchip Technology Inc.
|
|
|
|
@File Name
|
|
eusart1.c
|
|
|
|
@Summary
|
|
This is the generated driver implementation file for the EUSART1 driver using PIC10 / PIC12 / PIC16 / PIC18 MCUs
|
|
|
|
@Description
|
|
This source file provides APIs for EUSART1.
|
|
Generation Information :
|
|
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.8
|
|
Device : PIC18F97J60
|
|
Driver Version : 2.1.1
|
|
The generated drivers are tested against the following:
|
|
Compiler : XC8 2.36 and above
|
|
MPLAB : MPLAB X 6.00
|
|
*/
|
|
|
|
/*
|
|
(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 "eusart1.h"
|
|
|
|
/**
|
|
Section: Macro Declarations
|
|
*/
|
|
|
|
#define EUSART1_TX_BUFFER_SIZE 8
|
|
#define EUSART1_RX_BUFFER_SIZE 8
|
|
|
|
/**
|
|
Section: Global Variables
|
|
*/
|
|
|
|
volatile uint8_t eusart1RxHead = 0;
|
|
volatile uint8_t eusart1RxTail = 0;
|
|
volatile uint8_t eusart1RxBuffer[EUSART1_RX_BUFFER_SIZE];
|
|
volatile eusart1_status_t eusart1RxStatusBuffer[EUSART1_RX_BUFFER_SIZE];
|
|
volatile uint8_t eusart1RxCount;
|
|
volatile eusart1_status_t eusart1RxLastError;
|
|
|
|
/**
|
|
Section: EUSART1 APIs
|
|
*/
|
|
void (*EUSART1_RxDefaultInterruptHandler)(void);
|
|
|
|
void (*EUSART1_FramingErrorHandler)(void);
|
|
void (*EUSART1_OverrunErrorHandler)(void);
|
|
void (*EUSART1_ErrorHandler)(void);
|
|
|
|
void EUSART1_DefaultFramingErrorHandler(void);
|
|
void EUSART1_DefaultOverrunErrorHandler(void);
|
|
void EUSART1_DefaultErrorHandler(void);
|
|
|
|
void EUSART1_Initialize(void)
|
|
{
|
|
// disable interrupts before changing states
|
|
PIE1bits.RC1IE = 0;
|
|
EUSART1_SetRxInterruptHandler(EUSART1_Receive_ISR);
|
|
// Set the EUSART1 module to the options selected in the user interface.
|
|
|
|
// ABDOVF no_overflow; SCKP async_noninverted_sync_fallingedge; BRG16 16bit_generator; WUE disabled; ABDEN disabled; RXDTP not_inverted;
|
|
BAUDCON1 = 0x08;
|
|
|
|
// SPEN enabled; RX9 9-bit; CREN enabled; ADDEN disabled; SREN disabled;
|
|
RCSTA1 = 0xD0;
|
|
|
|
// TX9 9-bit; TX9D 1; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC master_mode;
|
|
TXSTA1 = 0xE5;
|
|
|
|
//
|
|
SPBRG1 = 0x8A;
|
|
|
|
//
|
|
SPBRGH1 = 0x02;
|
|
|
|
|
|
EUSART1_SetFramingErrorHandler(EUSART1_DefaultFramingErrorHandler);
|
|
EUSART1_SetOverrunErrorHandler(EUSART1_DefaultOverrunErrorHandler);
|
|
EUSART1_SetErrorHandler(EUSART1_DefaultErrorHandler);
|
|
|
|
eusart1RxLastError.status = 0;
|
|
|
|
|
|
eusart1RxHead = 0;
|
|
eusart1RxTail = 0;
|
|
eusart1RxCount = 0;
|
|
|
|
// enable receive interrupt
|
|
PIE1bits.RC1IE = 1;
|
|
}
|
|
|
|
bool EUSART1_is_tx_ready(void)
|
|
{
|
|
return (bool)(PIR1bits.TX1IF && TXSTA1bits.TXEN);
|
|
}
|
|
|
|
bool EUSART1_is_rx_ready(void)
|
|
{
|
|
return (eusart1RxCount ? true : false);
|
|
}
|
|
|
|
bool EUSART1_is_tx_done(void)
|
|
{
|
|
return TXSTA1bits.TRMT;
|
|
}
|
|
|
|
eusart1_status_t EUSART1_get_last_status(void){
|
|
return eusart1RxLastError;
|
|
}
|
|
|
|
uint8_t EUSART1_Read(void)
|
|
{
|
|
uint8_t readValue = 0;
|
|
|
|
while(0 == eusart1RxCount)
|
|
{
|
|
}
|
|
|
|
eusart1RxLastError = eusart1RxStatusBuffer[eusart1RxTail];
|
|
|
|
readValue = eusart1RxBuffer[eusart1RxTail++];
|
|
if(sizeof(eusart1RxBuffer) <= eusart1RxTail)
|
|
{
|
|
eusart1RxTail = 0;
|
|
}
|
|
PIE1bits.RC1IE = 0;
|
|
eusart1RxCount--;
|
|
PIE1bits.RC1IE = 1;
|
|
|
|
return readValue;
|
|
}
|
|
|
|
void EUSART1_Write(uint8_t txData)
|
|
{
|
|
while(0 == PIR1bits.TX1IF)
|
|
{
|
|
}
|
|
|
|
TXREG1 = txData; // Write the data byte to the USART.
|
|
}
|
|
|
|
|
|
|
|
void EUSART1_Receive_ISR(void)
|
|
{
|
|
|
|
eusart1RxStatusBuffer[eusart1RxHead].status = 0;
|
|
|
|
if(RCSTA1bits.FERR){
|
|
eusart1RxStatusBuffer[eusart1RxHead].ferr = 1;
|
|
EUSART1_FramingErrorHandler();
|
|
}
|
|
|
|
if(RCSTA1bits.OERR){
|
|
eusart1RxStatusBuffer[eusart1RxHead].oerr = 1;
|
|
EUSART1_OverrunErrorHandler();
|
|
}
|
|
|
|
if(eusart1RxStatusBuffer[eusart1RxHead].status){
|
|
EUSART1_ErrorHandler();
|
|
} else {
|
|
EUSART1_RxDataHandler();
|
|
}
|
|
|
|
// or set custom function using EUSART1_SetRxInterruptHandler()
|
|
}
|
|
|
|
void EUSART1_RxDataHandler(void){
|
|
// use this default receive interrupt handler code
|
|
eusart1RxBuffer[eusart1RxHead++] = RCREG1;
|
|
if(sizeof(eusart1RxBuffer) <= eusart1RxHead)
|
|
{
|
|
eusart1RxHead = 0;
|
|
}
|
|
eusart1RxCount++;
|
|
}
|
|
|
|
void EUSART1_DefaultFramingErrorHandler(void){}
|
|
|
|
void EUSART1_DefaultOverrunErrorHandler(void){
|
|
// EUSART1 error - restart
|
|
|
|
RCSTA1bits.CREN = 0;
|
|
RCSTA1bits.CREN = 1;
|
|
|
|
}
|
|
|
|
void EUSART1_DefaultErrorHandler(void){
|
|
EUSART1_RxDataHandler();
|
|
}
|
|
|
|
void EUSART1_SetFramingErrorHandler(void (* interruptHandler)(void)){
|
|
EUSART1_FramingErrorHandler = interruptHandler;
|
|
}
|
|
|
|
void EUSART1_SetOverrunErrorHandler(void (* interruptHandler)(void)){
|
|
EUSART1_OverrunErrorHandler = interruptHandler;
|
|
}
|
|
|
|
void EUSART1_SetErrorHandler(void (* interruptHandler)(void)){
|
|
EUSART1_ErrorHandler = interruptHandler;
|
|
}
|
|
|
|
|
|
void EUSART1_SetRxInterruptHandler(void (* interruptHandler)(void)){
|
|
EUSART1_RxDefaultInterruptHandler = interruptHandler;
|
|
}
|
|
/**
|
|
End of File
|
|
*/
|