Bar Logo 4kW dsPIC33C PSFB DC-DC DA (Part-No. )
 
Content
     
Loading...
Searching...
No Matches
uart1.c
Go to the documentation of this file.
1
17/*
18© [2024] Microchip Technology Inc. and its subsidiaries.
19
20 Subject to your compliance with these terms, you may use Microchip
21 software and any derivatives exclusively with Microchip products.
22 You are responsible for complying with 3rd party license terms
23 applicable to your use of 3rd party software (including open source
24 software) that may accompany Microchip software. SOFTWARE IS ?AS IS.?
25 NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS
26 SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,
27 MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
28 WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
29 INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY
30 KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
31 MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
32 FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S
33 TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT
34 EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR
35 THIS SOFTWARE.
36*/
37
38// Section: Included Files
39#include <stdint.h>
40#include <stddef.h>
41#include <xc.h>
42#include <stddef.h>
43#include "../uart1.h"
44
45// Section: Macro Definitions
46#define UART1_CLOCK 100000000U
47#define UART1_BAUD_TO_BRG_WITH_FRACTIONAL(x) (UART1_CLOCK/(x))
48#define UART1_BAUD_TO_BRG_WITH_BRGH_1(x) (UART1_CLOCK/(4U*(x))-1U)
49#define UART1_BAUD_TO_BRG_WITH_BRGH_0(x) (UART1_CLOCK/(16U*(x))-1U)
50#define UART1_BRG_TO_BAUD_WITH_FRACTIONAL(x) (UART1_CLOCK/(x))
51#define UART1_BRG_TO_BAUD_WITH_BRGH_1(x) (UART1_CLOCK/(4U*((x)+1U)))
52#define UART1_BRG_TO_BAUD_WITH_BRGH_0(x) (UART1_CLOCK/(16U*((x)+1U)))
53
54#define UART1_MIN_ACHIEVABLE_BAUD_WITH_FRACTIONAL 95U
55#define UART1_MIN_ACHIEVABLE_BAUD_WITH_BRGH_1 24U
56
57// Section: Driver Interface
58
59const struct UART_INTERFACE UART1_Drv = {
60 .Initialize = &UART1_Initialize,
61 .Deinitialize = &UART1_Deinitialize,
62 .Read = &UART1_Read,
63 .Write = &UART1_Write,
64 .IsRxReady = &UART1_IsRxReady,
65 .IsTxReady = &UART1_IsTxReady,
66 .IsTxDone = &UART1_IsTxDone,
67 .TransmitEnable = &UART1_TransmitEnable,
68 .TransmitDisable = &UART1_TransmitDisable,
69 .TransmitInterruptEnable = NULL,
70 .TransmitInterruptDisable = NULL,
71 .AutoBaudSet = &UART1_AutoBaudSet,
72 .AutoBaudQuery = &UART1_AutoBaudQuery,
73 .AutoBaudEventEnableGet = &UART1_AutoBaudEventEnableGet,
74 .BRGCountSet = &UART1_BRGCountSet,
75 .BRGCountGet = &UART1_BRGCountGet,
76 .BaudRateSet = &UART1_BaudRateSet,
77 .BaudRateGet = &UART1_BaudRateGet,
78 .ErrorGet = &UART1_ErrorGet,
79 .RxCompleteCallbackRegister = NULL,
80 .TxCompleteCallbackRegister = NULL,
81 .TxCollisionCallbackRegister = NULL,
82 .FramingErrorCallbackRegister = NULL,
83 .OverrunErrorCallbackRegister = NULL,
84 .ParityErrorCallbackRegister = NULL,
85};
86
87// Section: Private Variable Definitions
88static union
89{
90 struct
91 {
92 uint16_t frammingError :1;
93 uint16_t parityError :1;
94 uint16_t overrunError :1;
95 uint16_t txCollisionError :1;
96 uint16_t autoBaudOverflow :1;
97 uint16_t reserved :11;
98 };
99 size_t status;
101
102// Section: UART1 APIs
103
105{
106/*
107 Set the UART1 module to the options selected in the user interface.
108 Make sure to set LAT bit corresponding to TxPin as high before UART initialization
109*/
110 // URXEN ; RXBIMD ; UARTEN disabled; MOD Asynchronous 8-bit UART; UTXBRK ; BRKOVR ; UTXEN ; USIDL ; WAKE ; ABAUD ; BRGH ;
111 U1MODE = 0x0;
112 // STSEL 1 Stop bit sent, 1 checked at RX; BCLKMOD enabled; SLPEN ; FLO ; BCLKSEL FOSC/2; C0EN ; RUNOVF ; UTXINV ; URXINV ; HALFDPLX ;
113 U1MODEH = 0x800;
114 // OERIE ; RXBKIF ; RXBKIE ; ABDOVF ; OERR ; TXCIE ; TXCIF ; FERIE ; TXMTIE ; ABDOVE ; CERIE ; CERIF ; PERIE ;
115 U1STA = 0x80;
116 // URXISEL ; UTXBE ; UTXISEL ; URXBE ; STPMD ; TXWRE ;
117 U1STAH = 0x2E;
118 // BaudRate 115207.37; Frequency 100000000 Hz; BRG 868;
119 U1BRG = 0x364;
120 // BRG 0;
121 U1BRGH = 0x0;
122
123 U1MODEbits.UARTEN = 1; // enabling UART ON bit
124 U1MODEbits.UTXEN = 1;
125 U1MODEbits.URXEN = 1;
126}
127
129{
130 U1MODE = 0x0;
131 U1MODEH = 0x0;
132 U1STA = 0x80;
133 U1STAH = 0x2E;
134 U1BRG = 0x0;
135 U1BRGH = 0x0;
136}
137
138uint8_t UART1_Read(void)
139{
140 while((U1STAHbits.URXBE == 1))
141 {
142
143 }
144
145 if ((U1STAbits.OERR == 1))
146 {
147 U1STAbits.OERR = 0;
148 }
149
150 return U1RXREG;
151}
152
153void UART1_Write(uint8_t txData)
154{
155 while(U1STAHbits.UTXBF == 1)
156 {
157
158 }
159
160 U1TXREG = txData; // Write the data byte to the USART.
161}
162
164{
165 return (U1STAHbits.URXBE == 0);
166}
167
169{
170 return ((!U1STAHbits.UTXBF) && U1MODEbits.UTXEN);
171}
172
174{
175 return (bool)(U1STAbits.TRMT && U1STAHbits.UTXBE);
176}
177
179{
180 U1MODEbits.UTXEN = 1;
181}
182
184{
185 U1MODEbits.UTXEN = 0;
186}
187
188void UART1_AutoBaudSet(bool enable)
189{
190 U1INTbits.ABDIF = 0U;
191 U1INTbits.ABDIE = enable;
192 U1MODEbits.ABAUD = enable;
193}
194
196{
197 return U1MODEbits.ABAUD;
198}
199
201{
202 return U1INTbits.ABDIE;
203}
204
205size_t UART1_ErrorGet(void)
206{
207 uartError.status = 0;
208 if(U1STAbits.FERR == 1U)
209 {
211 }
212 if(U1STAbits.PERR== 1U)
213 {
215 }
216 if(U1STAbits.OERR== 1U)
217 {
219 U1STAbits.OERR = 0;
220 }
221 if(U1STAbits.TXCIF== 1U)
222 {
224 U1STAbits.TXCIF = 0;
225 }
226 if(U1STAbits.ABDOVF== 1U)
227 {
229 U1STAbits.ABDOVF = 0;
230 }
231
232 return uartError.status;
233}
234
235void UART1_BRGCountSet(uint32_t brgValue)
236{
237 U1BRG = brgValue & 0xFFFFU;
238 U1BRGH = (brgValue >>16U) & 0x000FU;
239}
240
241uint32_t UART1_BRGCountGet(void)
242{
243 uint32_t brgValue;
244
245 brgValue = U1BRGH;
246 brgValue = (brgValue << 16U) | U1BRG;
247
248 return brgValue;
249}
250
251void UART1_BaudRateSet(uint32_t baudRate)
252{
253 uint32_t brgValue;
254
255 if((baudRate >= UART1_MIN_ACHIEVABLE_BAUD_WITH_FRACTIONAL) && (baudRate != 0))
256 {
257 U1MODEHbits.BCLKMOD = 1;
258 U1MODEbits.BRGH = 0;
259 brgValue = UART1_BAUD_TO_BRG_WITH_FRACTIONAL(baudRate);
260 }
261 else if(baudRate >= UART1_MIN_ACHIEVABLE_BAUD_WITH_BRGH_1)
262 {
263 U1MODEHbits.BCLKMOD = 0;
264 U1MODEbits.BRGH = 1;
265 brgValue = UART1_BAUD_TO_BRG_WITH_BRGH_1(baudRate);
266 }
267 else
268 {
269 U1MODEHbits.BCLKMOD = 0;
270 U1MODEbits.BRGH = 0;
271 brgValue = UART1_BAUD_TO_BRG_WITH_BRGH_0(baudRate);
272 }
273 U1BRG = brgValue & 0xFFFFU;
274 U1BRGH = (brgValue >>16U) & 0x000FU;
275}
276
277uint32_t UART1_BaudRateGet(void)
278{
279 uint32_t brgValue;
280 uint32_t baudRate;
281
282 brgValue = UART1_BRGCountGet();
283 if((U1MODEHbits.BCLKMOD == 1) && (brgValue != 0))
284 {
285 baudRate = UART1_BRG_TO_BAUD_WITH_FRACTIONAL(brgValue);
286 }
287 else if(U1MODEbits.BRGH == 1)
288 {
289 baudRate = UART1_BRG_TO_BAUD_WITH_BRGH_1(brgValue);
290 }
291 else
292 {
293 baudRate = UART1_BRG_TO_BAUD_WITH_BRGH_0(brgValue);
294 }
295 return baudRate;
296}
#define UART1_BRG_TO_BAUD_WITH_BRGH_1(x)
Definition uart1.c:51
#define UART1_BRG_TO_BAUD_WITH_BRGH_0(x)
Definition uart1.c:52
uint16_t frammingError
Definition uart1.c:92
#define UART1_BRG_TO_BAUD_WITH_FRACTIONAL(x)
Definition uart1.c:50
uint16_t reserved
Definition uart1.c:97
size_t status
Definition uart1.c:99
#define UART1_MIN_ACHIEVABLE_BAUD_WITH_BRGH_1
Definition uart1.c:55
#define UART1_BAUD_TO_BRG_WITH_BRGH_1(x)
Definition uart1.c:48
uint16_t txCollisionError
Definition uart1.c:95
static union @1 uartError
#define UART1_BAUD_TO_BRG_WITH_FRACTIONAL(x)
Definition uart1.c:47
uint16_t parityError
Definition uart1.c:93
uint16_t autoBaudOverflow
Definition uart1.c:96
#define UART1_MIN_ACHIEVABLE_BAUD_WITH_FRACTIONAL
Definition uart1.c:54
#define UART1_BAUD_TO_BRG_WITH_BRGH_0(x)
Definition uart1.c:49
uint16_t overrunError
Definition uart1.c:94
This is the generated driver header file for the UART1 driver.
@ UART_ERROR_TX_COLLISION_MASK
Definition uart_types.h:51
@ UART_ERROR_PARITY_MASK
Definition uart_types.h:49
@ UART_ERROR_FRAMING_MASK
Definition uart_types.h:48
@ UART_ERROR_RX_OVERRUN_MASK
Definition uart_types.h:50
@ UART_ERROR_AUTOBAUD_OVERFLOW_MASK
Definition uart_types.h:52
void UART1_BaudRateSet(uint32_t baudRate)
Sets the calculated Baud-Rate of UART1.
Definition uart1.c:251
void UART1_Initialize(void)
Initializes the UART driver.
Definition uart1.c:104
void UART1_TransmitDisable(void)
Disables UART1 transmit.
Definition uart1.c:183
void UART1_AutoBaudSet(bool enable)
Enables or disables UART1 Auto-Baud detection.
Definition uart1.c:188
bool UART1_IsRxReady(void)
Returns a boolean value if data is available to read.
Definition uart1.c:163
void UART1_TransmitEnable(void)
Enables UART1 transmit.
Definition uart1.c:178
uint8_t UART1_Read(void)
Reads a byte of data from the UART1.
Definition uart1.c:138
const struct UART_INTERFACE UART1_Drv
Structure object of type UART_INTERFACE with the custom name given by the user in the Melody Driver U...
Definition uart1.c:59
bool UART1_IsTxDone(void)
Indicates if all bytes have been transferred.
Definition uart1.c:173
void UART1_Write(uint8_t txData)
Writes a byte of data to the UART1.
Definition uart1.c:153
size_t UART1_ErrorGet(void)
Returns the error status of UART1.
Definition uart1.c:205
void UART1_Deinitialize(void)
Deinitializes the UART to POR values.
Definition uart1.c:128
bool UART1_AutoBaudQuery(void)
Returns the status of Auto-Baud detection.
Definition uart1.c:195
bool UART1_AutoBaudEventEnableGet(void)
Returns enable state of the Auto-Baud feature.
Definition uart1.c:200
uint32_t UART1_BaudRateGet(void)
Gets the actual Baud-Rate of UART1.
Definition uart1.c:277
uint32_t UART1_BRGCountGet(void)
Gets the BRG value of UART1.
Definition uart1.c:241
bool UART1_IsTxReady(void)
Returns a boolean value if data can be written.
Definition uart1.c:168
void UART1_BRGCountSet(uint32_t brgValue)
Sets the BRG value of UART1.
Definition uart1.c:235
Structure containing the function pointers of UART driver.