-
Notifications
You must be signed in to change notification settings - Fork 0
/
eusart.c
172 lines (125 loc) · 4.39 KB
/
eusart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
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.81.8
Device : PIC18F46K20
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 "eusart.h"
volatile eusart_status_t eusartRxLastError;
/**
Section: EUSART APIs
*/
void (*EUSART_FramingErrorHandler)(void);
void (*EUSART_OverrunErrorHandler)(void);
void (*EUSART_ErrorHandler)(void);
void EUSART_DefaultFramingErrorHandler(void);
void EUSART_DefaultOverrunErrorHandler(void);
void EUSART_DefaultErrorHandler(void);
void EUSART_Initialize(void)
{
// Set the EUSART module to the options selected in the user interface.
// ABDOVF no_overflow; CKTXP async_noninverted_sync_fallingedge; BRG16 16bit_generator; WUE disabled; ABDEN disabled; DTRXP not_inverted;
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_mode;
TXSTA = 0x24;
//
SPBRG = 0xA0;
//
SPBRGH = 0x01;
EUSART_SetFramingErrorHandler(EUSART_DefaultFramingErrorHandler);
EUSART_SetOverrunErrorHandler(EUSART_DefaultOverrunErrorHandler);
EUSART_SetErrorHandler(EUSART_DefaultErrorHandler);
eusartRxLastError.status = 0;
}
bool EUSART_is_tx_ready(void)
{
return (bool)(PIR1bits.TX1IF && TXSTAbits.TXEN);
}
bool EUSART_is_rx_ready(void)
{
return (bool)(PIR1bits.RC1IF);
}
bool EUSART_is_tx_done(void)
{
return TXSTAbits.TRMT;
}
eusart_status_t EUSART_get_last_status(void){
return eusartRxLastError;
}
uint8_t EUSART_Read(void)
{
while(!PIR1bits.RC1IF)
{
}
eusartRxLastError.status = 0;
if(1 == RCSTAbits.OERR)
{
// EUSART error - restart
RCSTAbits.CREN = 0;
RCSTAbits.CREN = 1;
}
return RCREG;
}
void EUSART_Write(uint8_t txData)
{
while(0 == PIR1bits.TX1IF)
{
}
TXREG = txData; // Write the data byte to the USART.
}
void EUSART_DefaultFramingErrorHandler(void){}
void EUSART_DefaultOverrunErrorHandler(void){
// EUSART error - restart
RCSTAbits.CREN = 0;
RCSTAbits.CREN = 1;
}
void EUSART_DefaultErrorHandler(void){
}
void EUSART_SetFramingErrorHandler(void (* interruptHandler)(void)){
EUSART_FramingErrorHandler = interruptHandler;
}
void EUSART_SetOverrunErrorHandler(void (* interruptHandler)(void)){
EUSART_OverrunErrorHandler = interruptHandler;
}
void EUSART_SetErrorHandler(void (* interruptHandler)(void)){
EUSART_ErrorHandler = interruptHandler;
}
/**
End of File
*/