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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/**
UART1 Generated Driver File
@Company
Microchip Technology Inc.
@File Name
uart1.c
@Summary
This is the generated source file for the UART1 driver using PIC32MX MCUs
@Description
This source file provides APIs for driver for UART1.
Generation Information :
Product Revision : PIC32MX MCUs - pic32mx : v1.35
Device : PIC32MX470F512H
Driver Version : 0.5
The generated drivers are tested against the following:
Compiler : XC32 1.42
MPLAB : MPLAB X 3.55
*/
/*
(c) 2016 Microchip Technology Inc. and its subsidiaries. You may use this
software and any derivatives exclusively with Microchip products.
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, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
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.
MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
TERMS.
*/
/**
Section: Included Files
*/
#include "uart1.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
Section: Data Type Definitions
*/
/** UART Driver Queue Status
@Summary
Defines the object required for the status of the queue.
*/
typedef union
{
struct
{
uint8_t full:1;
uint8_t empty:1;
uint8_t reserved:6;
} s;
uint8_t status;
} UART_BYTEQ_STATUS;
/** UART Driver Hardware Instance Object
@Summary
Defines the object required for the maintenance of the hardware instance.
*/
typedef struct
{
/* RX Byte Q */
uint8_t *rxTail;
uint8_t *rxHead;
/* TX Byte Q */
uint8_t *txTail;
uint8_t *txHead;
UART_BYTEQ_STATUS rxStatus;
UART_BYTEQ_STATUS txStatus;
} UART_OBJECT;
static UART_OBJECT uart1_obj;
/** UART Driver Queue Length
@Summary
Defines the length of the Transmit and Receive Buffers
*/
#define UART1_CONFIG_TX_BYTEQ_LENGTH 8
#define UART1_CONFIG_RX_BYTEQ_LENGTH 8
/** UART Driver Queue
@Summary
Defines the Transmit and Receive Buffers
*/
static uint8_t uart1_txByteQ[UART1_CONFIG_TX_BYTEQ_LENGTH];
static uint8_t uart1_rxByteQ[UART1_CONFIG_RX_BYTEQ_LENGTH];
/** UART Hardware FIFO Buffer Length
@Summary
Defines the length of the Transmit and Receive FIFOs
*/
#define UART1_TX_FIFO_LENGTH 1
#define UART1_RX_FIFO_LENGTH 1
/**
Section: Driver Interface
*/
void UART1_Initialize (void)
{
// STSEL 1S; IREN disabled; PDSEL 8N; RTSMD disabled; RXINV disabled; SIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH enabled; UEN TX_RX; ON enabled;
U1MODE = 0x8008;
// UTXISEL TX_ONE_CHAR; UTXINV disabled; ADDR 0; URXEN disabled; OERR disabled; ADM_EN disabled; URXISEL RX_ONE_CHAR; UTXBRK disabled; UTXEN disabled; ADDEN disabled;
U1STA = 0x0;
// U1TXREG 0;
U1TXREG = 0x0;
// BaudRate = 9600; Frequency = 1000000 Hz; BRG 25;
U1BRG = 0x19;
IEC1bits.U1RXIE = 1;
U1STAbits.UTXEN = 1;
U1STAbits.URXEN = 1;
//Enabling UART
U1MODEbits.ON = 1;
uart1_obj.txHead = uart1_txByteQ;
uart1_obj.txTail = uart1_txByteQ;
uart1_obj.rxHead = uart1_rxByteQ;
uart1_obj.rxTail = uart1_rxByteQ;
uart1_obj.rxStatus.s.empty = true;
uart1_obj.txStatus.s.empty = true;
uart1_obj.txStatus.s.full = false;
uart1_obj.rxStatus.s.full = false;
}
/**
Maintains the driver's transmitter/receiver/error state machine and implements its ISR
*/
void __ISR(_UART_1_VECTOR, IPL1AUTO) _UART_1 (void)
{
if(IFS1bits.U1RXIF) {
int count = 0;
while((count < UART1_RX_FIFO_LENGTH) && (U1STAbits.URXDA == 1)) {
count++;
*uart1_obj.rxTail = U1RXREG;
uart1_obj.rxTail++;
if(uart1_obj.rxTail == (uart1_rxByteQ + UART1_CONFIG_RX_BYTEQ_LENGTH)) {
uart1_obj.rxTail = uart1_rxByteQ;
}
uart1_obj.rxStatus.s.empty = false;
if(uart1_obj.rxTail == uart1_obj.rxHead) {
//Sets the flag RX full
uart1_obj.rxStatus.s.full = true;
break;
}
}
IFS1CLR= 1 << _IFS1_U1RXIF_POSITION;
} else if (IFS1bits.U1TXIF) {
if(uart1_obj.txStatus.s.empty) {
IEC1bits.U1TXIE = false;
return;
}
IFS1CLR= 1 << _IFS1_U1TXIF_POSITION;
int count = 0;
while((count < UART1_TX_FIFO_LENGTH)&& !(U1STAbits.UTXBF == 1)) {
count++;
U1TXREG = *uart1_obj.txHead;
uart1_obj.txHead++;
if(uart1_obj.txHead == (uart1_txByteQ + UART1_CONFIG_TX_BYTEQ_LENGTH)) {
uart1_obj.txHead = uart1_txByteQ;
}
uart1_obj.txStatus.s.full = false;
if(uart1_obj.txHead == uart1_obj.txTail) {
uart1_obj.txStatus.s.empty = true;
break;
}
}
} else {
if ((U1STAbits.OERR == 1)) {
U1STAbits.OERR = 0;
}
IFS1CLR= 1 << _IFS1_U1EIF_POSITION;
}
}
/**
Section: UART Driver Client Routines
*/
uint8_t UART1_Read(void)
{
uint8_t data = 0;
data = *uart1_obj.rxHead;
uart1_obj.rxHead++;
if (uart1_obj.rxHead == (uart1_rxByteQ + UART1_CONFIG_RX_BYTEQ_LENGTH)) {
uart1_obj.rxHead = uart1_rxByteQ;
}
if (uart1_obj.rxHead == uart1_obj.rxTail) {
uart1_obj.rxStatus.s.empty = true;
}
uart1_obj.rxStatus.s.full = false;
return data;
}
unsigned int UART1_ReadBuffer(uint8_t *buffer, const unsigned int bufLen)
{
unsigned int numBytesRead = 0;
while (numBytesRead < ( bufLen )) {
if(uart1_obj.rxStatus.s.empty) {
break;
} else {
buffer[numBytesRead++] = UART1_Read () ;
}
}
return numBytesRead ;
}
void UART1_Write(const uint8_t byte)
{
IEC1bits.U1TXIE = false;
*uart1_obj.txTail = byte;
uart1_obj.txTail++;
if (uart1_obj.txTail == (uart1_txByteQ + UART1_CONFIG_TX_BYTEQ_LENGTH)) {
uart1_obj.txTail = uart1_txByteQ;
}
uart1_obj.txStatus.s.empty = false;
if (uart1_obj.txHead == uart1_obj.txTail) {
uart1_obj.txStatus.s.full = true;
}
IEC1bits.U1TXIE = true ;
}
unsigned int UART1_WriteBuffer(const uint8_t *buffer , const unsigned int bufLen )
{
unsigned int numBytesWritten = 0 ;
while ( numBytesWritten < ( bufLen )) {
if((uart1_obj.txStatus.s.full)) {
break;
} else {
UART1_Write (buffer[numBytesWritten++] ) ;
}
}
return numBytesWritten ;
}
UART1_TRANSFER_STATUS UART1_TransferStatusGet (void)
{
uint8_t status = 0;
if(uart1_obj.txStatus.s.full) {
status |= UART1_TRANSFER_STATUS_TX_FULL;
}
if(uart1_obj.txStatus.s.empty) {
status |= UART1_TRANSFER_STATUS_TX_EMPTY;
}
if(uart1_obj.rxStatus.s.full) {
status |= UART1_TRANSFER_STATUS_RX_FULL;
}
if(uart1_obj.rxStatus.s.empty) {
status |= UART1_TRANSFER_STATUS_RX_EMPTY;
} else {
status |= UART1_TRANSFER_STATUS_RX_DATA_PRESENT;
}
return (UART1_TRANSFER_STATUS) status;
}
uint8_t UART1_Peek(uint16_t offset)
{
if( (uart1_obj.rxHead + offset) > (uart1_rxByteQ + UART1_CONFIG_RX_BYTEQ_LENGTH)) {
return uart1_rxByteQ[offset - (uart1_rxByteQ + UART1_CONFIG_RX_BYTEQ_LENGTH - uart1_obj.rxHead)];
} else {
return *(uart1_obj.rxHead + offset);
}
}
unsigned int UART1_ReceiveBufferSizeGet(void)
{
if(!uart1_obj.rxStatus.s.full) {
if(uart1_obj.rxHead > uart1_obj.rxTail) {
return(uart1_obj.rxHead - uart1_obj.rxTail);
} else {
return(UART1_CONFIG_RX_BYTEQ_LENGTH - (uart1_obj.rxTail - uart1_obj.rxHead));
}
}
return 0;
}
unsigned int UART1_TransmitBufferSizeGet(void)
{
if(!uart1_obj.txStatus.s.full) {
if(uart1_obj.txHead > uart1_obj.txTail) {
return (uart1_obj.txHead - uart1_obj.txTail);
} else {
return (UART1_CONFIG_TX_BYTEQ_LENGTH - (uart1_obj.txTail - uart1_obj.txHead));
}
}
return 0;
}
bool UART1_ReceiveBufferIsEmpty (void)
{
return(uart1_obj.rxStatus.s.empty);
}
bool UART1_TransmitBufferIsFull(void)
{
return(uart1_obj.txStatus.s.full);
}
UART1_STATUS UART1_StatusGet (void)
{
return (UART1_STATUS) U1STA;
}
/**
End of File
*/
#ifdef __cplusplus
}
#endif
|