root/RF_BT_Tail/mcc_generated_files/eusart.c

Revision 244, 5.4 kB (checked in by phil, 4 years ago)

added initial Tail Source Package

Line 
1 /**
2   EUSART Generated Driver File
3
4   @Company
5     Microchip Technology Inc.
6
7   @File Name
8     eusart.c
9
10   @Summary
11     This is the generated driver implementation file for the EUSART driver using PIC10 / PIC12 / PIC16 / PIC18 MCUs
12
13   @Description
14     This source file provides APIs for EUSART.
15     Generation Information :
16         Product Revision  :  PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.65.2
17         Device            :  PIC16F1579
18         Driver Version    :  2.01
19     The generated drivers are tested against the following:
20         Compiler          :  XC8 1.45
21         MPLAB             :  MPLAB X 4.15
22 */
23
24 /*
25     (c) 2018 Microchip Technology Inc. and its subsidiaries.
26    
27     Subject to your compliance with these terms, you may use Microchip software and any
28     derivatives exclusively with Microchip products. It is your responsibility to comply with third party
29     license terms applicable to your use of third party software (including open source software) that
30     may accompany Microchip software.
31    
32     THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
33     EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
34     IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
35     FOR A PARTICULAR PURPOSE.
36    
37     IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
38     INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
39     WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
40     HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
41     THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
42     CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
43     OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
44     SOFTWARE.
45 */
46
47 /**
48   Section: Included Files
49 */
50 #include "eusart.h"
51
52 /**
53   Section: Macro Declarations
54 */
55
56 #define EUSART_TX_BUFFER_SIZE 16
57 #define EUSART_RX_BUFFER_SIZE 64
58
59 /**
60   Section: Global Variables
61 */
62 volatile uint8_t eusartTxHead = 0;
63 volatile uint8_t eusartTxTail = 0;
64 volatile uint8_t eusartTxBuffer[EUSART_TX_BUFFER_SIZE];
65 volatile uint8_t eusartTxBufferRemaining;
66
67 volatile uint8_t eusartRxHead = 0;
68 volatile uint8_t eusartRxTail = 0;
69 volatile uint8_t eusartRxBuffer[EUSART_RX_BUFFER_SIZE];
70 volatile uint8_t eusartRxCount;
71
72 /**
73   Section: EUSART APIs
74 */
75 void EUSART_Initialize(void)
76 {
77     // disable interrupts before changing states
78     PIE1bits.RCIE = 0;
79     EUSART_SetRxInterruptHandler(EUSART_Receive_ISR);
80     PIE1bits.TXIE = 0;
81     EUSART_SetTxInterruptHandler(EUSART_Transmit_ISR);
82     // Set the EUSART module to the options selected in the user interface.
83
84     // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
85     BAUDCON = 0x08;
86
87     // SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled;
88     RCSTA = 0x90;
89
90     // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
91     TXSTA = 0x24;
92
93     // SPBRGL 16;
94     SPBRGL = 0x10;
95
96     // SPBRGH 0;
97     SPBRGH = 0x00;
98
99
100     // initializing the driver state
101     eusartTxHead = 0;
102     eusartTxTail = 0;
103     eusartTxBufferRemaining = sizeof(eusartTxBuffer);
104
105     eusartRxHead = 0;
106     eusartRxTail = 0;
107     eusartRxCount = 0;
108
109     // enable receive interrupt
110     PIE1bits.RCIE = 1;
111 }
112
113 uint8_t EUSART_is_tx_ready(void)
114 {
115     return eusartTxBufferRemaining;
116 }
117
118 uint8_t EUSART_is_rx_ready(void)
119 {
120     return eusartRxCount;
121 }
122
123 bool EUSART_is_tx_done(void)
124 {
125     return TXSTAbits.TRMT;
126 }
127
128 uint8_t EUSART_Read(void)
129 {
130     uint8_t readValue  = 0;
131    
132     while(0 == eusartRxCount)
133     {
134     }
135
136     readValue = eusartRxBuffer[eusartRxTail++];
137     if(sizeof(eusartRxBuffer) <= eusartRxTail)
138     {
139         eusartRxTail = 0;
140     }
141     PIE1bits.RCIE = 0;
142     eusartRxCount--;
143     PIE1bits.RCIE = 1;
144
145     return readValue;
146 }
147
148 void EUSART_Write(uint8_t txData)
149 {
150     while(0 == eusartTxBufferRemaining)
151     {
152     }
153
154     if(0 == PIE1bits.TXIE)
155     {
156         TXREG = txData;
157     }
158     else
159     {
160         PIE1bits.TXIE = 0;
161         eusartTxBuffer[eusartTxHead++] = txData;
162         if(sizeof(eusartTxBuffer) <= eusartTxHead)
163         {
164             eusartTxHead = 0;
165         }
166         eusartTxBufferRemaining--;
167     }
168     PIE1bits.TXIE = 1;
169 }
170
171
172 void EUSART_Transmit_ISR(void)
173 {
174
175     // add your EUSART interrupt custom code
176     if(sizeof(eusartTxBuffer) > eusartTxBufferRemaining)
177     {
178         TXREG = eusartTxBuffer[eusartTxTail++];
179         if(sizeof(eusartTxBuffer) <= eusartTxTail)
180         {
181             eusartTxTail = 0;
182         }
183         eusartTxBufferRemaining++;
184     }
185     else
186     {
187         PIE1bits.TXIE = 0;
188     }
189 }
190
191 void EUSART_Receive_ISR(void)
192 {
193    
194     if(1 == RCSTAbits.OERR)
195     {
196         // EUSART error - restart
197
198         RCSTAbits.CREN = 0;
199         RCSTAbits.CREN = 1;
200     }
201
202     // buffer overruns are ignored
203     eusartRxBuffer[eusartRxHead++] = RCREG;
204     if(sizeof(eusartRxBuffer) <= eusartRxHead)
205     {
206         eusartRxHead = 0;
207     }
208     eusartRxCount++;
209 }
210
211 void EUSART_SetTxInterruptHandler(void (* interruptHandler)(void)){
212     EUSART_TxDefaultInterruptHandler = interruptHandler;
213 }
214
215 void EUSART_SetRxInterruptHandler(void (* interruptHandler)(void)){
216     EUSART_RxDefaultInterruptHandler = interruptHandler;
217 }
218 /**
219   End of File
220 */
Note: See TracBrowser for help on using the browser.