Bar Logo 3.8/7.6 kw Totem pole Demonstration Application (Part-No. (not specified))
 
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 = {
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
111//TODO: melody settings below
112
113 // URXEN ; RXBIMD ; UARTEN disabled; MOD Asynchronous 8-bit UART; UTXBRK ; BRKOVR ; UTXEN ; USIDL ; WAKE ; ABAUD ; BRGH ;
114// U1MODE = 0x0;
115// // STSEL 1 Stop bit sent, 1 checked at RX; BCLKMOD enabled; SLPEN ; FLO ; BCLKSEL FOSC/2; C0EN ; RUNOVF ; UTXINV ; URXINV ; HALFDPLX ;
116// U1MODEH = 0x800;
117// // OERIE ; RXBKIF ; RXBKIE ; ABDOVF ; OERR ; TXCIE ; TXCIF ; FERIE ; TXMTIE ; ABDOVE ; CERIE ; CERIF ; PERIE ;
118// U1STA = 0x80;
119// // URXISEL ; UTXBE ; UTXISEL ; URXBE ; STPMD ; TXWRE ;
120// U1STAH = 0x2E;
121// // BaudRate 115207.37; Frequency 100000000 Hz; BRG 868;
122// U1BRG = 0x364;
123// // BRG 0;
124// U1BRGH = 0x0;
125//
126// U1MODEbits.UARTEN = 1; // enabling UART ON bit
127// U1MODEbits.UTXEN = 1;
128// U1MODEbits.URXEN = 1;
129
130// TODO: melody settings above
131
132// TODO: MCC settings below
133
138 // URXEN disabled; RXBIMD RXBKIF flag when Break makes low-to-high transition after being low for at least 23/11 bit periods; UARTEN enabled; MOD Asynchronous 8-bit UART; UTXBRK disabled; BRKOVR TX line driven by shifter; UTXEN disabled; USIDL disabled; WAKE disabled; ABAUD disabled; BRGH enabled;
139 // Data Bits = 8; Parity = None; Stop Bits = 1 Stop bit sent, 1 checked at RX;
140 U1MODE = (0x8080 & ~(1<<15)); // disabling UARTEN bit
141 // STSEL 1 Stop bit sent, 1 checked at RX; BCLKMOD disabled; SLPEN disabled; FLO Off; BCLKSEL FOSC/2; C0EN disabled; RUNOVF disabled; UTXINV disabled; URXINV disabled; HALFDPLX disabled;
142 U1MODEH = 0x00;
143 // OERIE disabled; RXBKIF disabled; RXBKIE disabled; ABDOVF disabled; OERR disabled; TXCIE disabled; TXCIF disabled; FERIE disabled; TXMTIE disabled; ABDOVE disabled; CERIE disabled; CERIF disabled; PERIE disabled;
144 U1STA = 0x00;
145 // URXISEL RX_ONE_WORD; UTXBE enabled; UTXISEL TX_BUF_EMPTY; URXBE enabled; STPMD disabled; TXWRE disabled;
146 U1STAH = 0x22;
147 // BaudRate = 115200; Frequency = 100000000 Hz; BRG 216;
148 U1BRG = 0xD8;
149 // BRG 0;
150 U1BRGH = 0x00;
151 // P1 0;
152 U1P1 = 0x00;
153 // P2 0;
154 U1P2 = 0x00;
155 // P3 0;
156 U1P3 = 0x00;
157 // P3H 0;
158 U1P3H = 0x00;
159 // TXCHK 0;
160 U1TXCHK = 0x00;
161 // RXCHK 0;
162 U1RXCHK = 0x00;
163 // T0PD 1 ETU; PTRCL disabled; TXRPT Retransmit the error byte once; CONV Direct logic;
164 U1SCCON = 0x00;
165 // TXRPTIF disabled; TXRPTIE disabled; WTCIF disabled; WTCIE disabled; BTCIE disabled; BTCIF disabled; GTCIF disabled; GTCIE disabled; RXRPTIE disabled; RXRPTIF disabled;
166 U1SCINT = 0x00;
167 // ABDIF disabled; WUIF disabled; ABDIE disabled;
168 U1INT = 0x00;
169
170 U1MODEbits.UARTEN = 1; // enabling UART ON bit
171 U1MODEbits.UTXEN = 1;
172 U1MODEbits.URXEN = 1;
173// TODO: MCC settings above
174}
175
177{
178 U1MODE = 0x0;
179 U1MODEH = 0x0;
180 U1STA = 0x80;
181 U1STAH = 0x2E;
182 U1BRG = 0x0;
183 U1BRGH = 0x0;
184}
185
186uint8_t UART1_Read(void)
187{
188 while((U1STAHbits.URXBE == 1))
189 {
190
191 }
192
193 if ((U1STAbits.OERR == 1))
194 {
195 U1STAbits.OERR = 0;
196 }
197
198 return U1RXREG;
199}
200
201void UART1_Write(uint8_t txData)
202{
203 while(U1STAHbits.UTXBF == 1)
204 {
205
206 }
207
208 U1TXREG = txData; // Write the data byte to the USART.
209}
210
212{
213 return (U1STAHbits.URXBE == 0);
214}
215
217{
218 return ((!U1STAHbits.UTXBF) && U1MODEbits.UTXEN);
219}
220
222{
223 return (bool)(U1STAbits.TRMT && U1STAHbits.UTXBE);
224}
225
227{
228 U1MODEbits.UTXEN = 1;
229}
230
232{
233 U1MODEbits.UTXEN = 0;
234}
235
236void UART1_AutoBaudSet(bool enable)
237{
238 U1INTbits.ABDIF = 0U;
239 U1INTbits.ABDIE = enable;
240 U1MODEbits.ABAUD = enable;
241}
242
244{
245 return U1MODEbits.ABAUD;
246}
247
249{
250 return U1INTbits.ABDIE;
251}
252
253size_t UART1_ErrorGet(void)
254{
255 uartError.status = 0;
256 if(U1STAbits.FERR == 1U)
257 {
259 }
260 if(U1STAbits.PERR== 1U)
261 {
263 }
264 if(U1STAbits.OERR== 1U)
265 {
267 U1STAbits.OERR = 0;
268 }
269 if(U1STAbits.TXCIF== 1U)
270 {
272 U1STAbits.TXCIF = 0;
273 }
274 if(U1STAbits.ABDOVF== 1U)
275 {
277 U1STAbits.ABDOVF = 0;
278 }
279
280 return uartError.status;
281}
282
283void UART1_BRGCountSet(uint32_t brgValue)
284{
285 U1BRG = brgValue & 0xFFFFU;
286 U1BRGH = (brgValue >>16U) & 0x000FU;
287}
288
289uint32_t UART1_BRGCountGet(void)
290{
291 uint32_t brgValue;
292
293 brgValue = U1BRGH;
294 brgValue = (brgValue << 16U) | U1BRG;
295
296 return brgValue;
297}
298
299void UART1_BaudRateSet(uint32_t baudRate)
300{
301 uint32_t brgValue;
302
303 if((baudRate >= UART1_MIN_ACHIEVABLE_BAUD_WITH_FRACTIONAL) && (baudRate != 0))
304 {
305 U1MODEHbits.BCLKMOD = 1;
306 U1MODEbits.BRGH = 0;
307 brgValue = UART1_BAUD_TO_BRG_WITH_FRACTIONAL(baudRate);
308 }
309 else if(baudRate >= UART1_MIN_ACHIEVABLE_BAUD_WITH_BRGH_1)
310 {
311 U1MODEHbits.BCLKMOD = 0;
312 U1MODEbits.BRGH = 1;
313 brgValue = UART1_BAUD_TO_BRG_WITH_BRGH_1(baudRate);
314 }
315 else
316 {
317 U1MODEHbits.BCLKMOD = 0;
318 U1MODEbits.BRGH = 0;
319 brgValue = UART1_BAUD_TO_BRG_WITH_BRGH_0(baudRate);
320 }
321 U1BRG = brgValue & 0xFFFFU;
322 U1BRGH = (brgValue >>16U) & 0x000FU;
323}
324
325uint32_t UART1_BaudRateGet(void)
326{
327 uint32_t brgValue;
328 uint32_t baudRate;
329
330 brgValue = UART1_BRGCountGet();
331 if((U1MODEHbits.BCLKMOD == 1) && (brgValue != 0))
332 {
333 baudRate = UART1_BRG_TO_BAUD_WITH_FRACTIONAL(brgValue);
334 }
335 else if(U1MODEbits.BRGH == 1)
336 {
337 baudRate = UART1_BRG_TO_BAUD_WITH_BRGH_1(brgValue);
338 }
339 else
340 {
341 baudRate = UART1_BRG_TO_BAUD_WITH_BRGH_0(brgValue);
342 }
343 return baudRate;
344}
#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
static union @21 uartError
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
#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:299
void UART1_Initialize(void)
Initializes the UART driver.
Definition uart1.c:104
void UART1_TransmitDisable(void)
Disables UART1 transmit.
Definition uart1.c:231
void UART1_AutoBaudSet(bool enable)
Enables or disables UART1 Auto-Baud detection.
Definition uart1.c:236
bool UART1_IsRxReady(void)
Returns a boolean value if data is available to read.
Definition uart1.c:211
void UART1_TransmitEnable(void)
Enables UART1 transmit.
Definition uart1.c:226
uint8_t UART1_Read(void)
Reads a byte of data from the UART1.
Definition uart1.c:186
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:221
void UART1_Write(uint8_t txData)
Writes a byte of data to the UART1.
Definition uart1.c:201
size_t UART1_ErrorGet(void)
Returns the error status of UART1.
Definition uart1.c:253
void UART1_Deinitialize(void)
Deinitializes the UART to POR values.
Definition uart1.c:176
bool UART1_AutoBaudQuery(void)
Returns the status of Auto-Baud detection.
Definition uart1.c:243
bool UART1_AutoBaudEventEnableGet(void)
Returns enable state of the Auto-Baud feature.
Definition uart1.c:248
uint32_t UART1_BaudRateGet(void)
Gets the actual Baud-Rate of UART1.
Definition uart1.c:325
uint32_t UART1_BRGCountGet(void)
Gets the BRG value of UART1.
Definition uart1.c:289
bool UART1_IsTxReady(void)
Returns a boolean value if data can be written.
Definition uart1.c:216
void UART1_BRGCountSet(uint32_t brgValue)
Sets the BRG value of UART1.
Definition uart1.c:283
Structure containing the function pointers of UART driver.
void(* Initialize)(void)
Pointer to UARTx_Initialize e.g. UART1_Initialize.