Bar Logo Dual Active Bridge Development Board (Part-No. )
 
Content
     
Loading...
Searching...
No Matches
PBV_UART.c
1
2
3/*
4 * @file app_PBV_UART.c
5 * @ingroup PBV_UART
6 * @brief Power Board Visualizer UART interface
7 */
8
9//includes
10#include <stdint.h>
11#include <stdbool.h>
12
13#include "PBV_UART.h"
14// MCC header files
15
16#include "uart/uart1.h"
17#include "crc/crc.h"
18
19
24#define PBV_CRC_POLYNOM 0x8005
25
26
30#define PBV_CRC_POLYNOM_REV 0xA001
31
32
36#define PBV_ReadyToSend UART1_IsTxReady
37
38
42#define PBV_Write UART1_Write
43
44
48#define PBV_IsRxReady UART1_IsRxReady
49
50
54#define PBV_Read UART1_Read
55
56
60#define PBV_START_OF_FRAME 0x55
61
62
66#define PBV_END_OF_FRAME 0x0d
67
68
74#define RCV_WAIT_FOR_STARTBYTE 0
75#define RCV_READ_ID_HIGHBYTE 1
76#define RCV_READ_ID_LOWBYTE 2
77#define RCV_READ_LENGTH_HIGHBYTE 3
78#define RCV_READ_LENGTH_LOWBYTE 4
79#define RCV_READ_DATA 5
80#define RCV_READ_CRC_HIGHBYTE 6
81#define RCV_READ_CRC_LOWBYTE 7
82#define RCV_READ_EOF 8
83#define RCV_MESSAGE_RECEIVED 9
86
91#define PBV_RCV_DATABUFFER_SIZE 64 // MAX..
92
93
98#define PBV_HEADER_SIZE 5
99
100
114
115
119typedef struct UART_MSG_TX_OBJ
120{
123 uint16_t Protocol_ID;
124 uint16_t Offset;
125 uint16_t CRC;
127 uint8_t * data;
129
130uint32_t PBV_calculated_CRC = 0;
131
132static bool uartActiveTx;
133static bool uartActiveTxAscii;
134
135// private function declaration
136uint16_t PBV_Calculate_CRC(uint8_t *message, uint16_t length);
137
138
143
144
149
150
155
156
165void PBV_UART_Init(PBV_Datatype_TX_t * boardToPbv, PBV_Datatype_TX_t * boardToPbvAscii, PBV_Datatype_RX_t * pbvToBoard)
166{
170 pbvUartObjectTx.data = boardToPbv->Data_Buffer;
171
173
176 pbvUartObjectAscii.Length_in_Bytes = boardToPbvAscii->Length;
177 pbvUartObjectAscii.data = boardToPbvAscii->Data_Buffer;
178
179 uartActiveTx = false;
180 uartActiveTxAscii = false;
181}
182
183
190{
191 static uint16_t rcv_data_index = 0;
192 static uint16_t rcv_CRC = 0;
193 static uint16_t rcv_timeout = 0;
194 static uint8_t rcv_copy_for_CRC[PBV_RCV_DATABUFFER_SIZE + PBV_HEADER_SIZE];
195 uint8_t data;
196
197 while (1)
198 {
199 if (PBV_IsRxReady() == false)
200 {
201 if (++rcv_timeout >= 10000)
202 {
203 rcv_timeout = 0;
205 }
207 }
208 // ok, we have some data coming in, lets read and process
209 rcv_timeout = 0;
210 data = PBV_Read();
212 {
214 if (data == PBV_START_OF_FRAME)
215 {
219 rcv_copy_for_CRC[rcv_data_index] = data; // for CRC
220 rcv_data_index++; // for CRC
222 }
223 return PBV_STATE_RECEIVING;
224
226 pbvUartObjectRx.Protocol_ID = data << 8;
229 rcv_copy_for_CRC[rcv_data_index] = data; // for CRC
230 rcv_data_index++; // for CRC
232 return PBV_STATE_RECEIVING;
233
238 rcv_copy_for_CRC[rcv_data_index] = data; // for CRC
239 rcv_data_index++; // for CRC
241 return PBV_STATE_RECEIVING;
242
244 // clear CRC flag, for now, as the firmware doesn't support it
245 // With PBV version 2.0.0, dsPIC cannot receive
246 // messages from the GUI if this flag is not cleared
248 rcv_copy_for_CRC[rcv_data_index] = data; // for CRC
249 rcv_data_index++; // for CRC
250 data = data & 0x7F; // clear bit 7 (CRC flag)
254 return PBV_STATE_RECEIVING;
255
259 rcv_copy_for_CRC[rcv_data_index] = data; // for CRC
260 rcv_data_index++; // for CRC
263 return PBV_STATE_RECEIVING;
264
265 case RCV_READ_DATA:
267 {
270
271 rcv_copy_for_CRC[rcv_data_index] = data; // for CRC
272 rcv_data_index++; // for CRC
273 }
274 if (pbvUartObjectRx.Offset >= pbvUartObjectRx.Length_in_Bytes) //are we finished receiving data???
275 {
277 }
278 return PBV_STATE_RECEIVING;
279
281 rcv_CRC = data << 8;
283 return PBV_STATE_RECEIVING;
284
286 rcv_CRC |= data;
288 return PBV_STATE_RECEIVING;
289
290 case RCV_READ_EOF:
291 if (data == 0x0d)
292 {
294 PBV_calculated_CRC = PBV_Calculate_CRC(rcv_copy_for_CRC, pbvUartObjectRx.Length_in_Bytes + PBV_HEADER_SIZE);
295 if (PBV_calculated_CRC == rcv_CRC)
296 {
297 rcv_data_index = 0;
298 rcv_CRC = 0;
299 PBV_calculated_CRC = 0;
302 }
303 else {
304 rcv_data_index = 0; //for CRC
306 }
307 }
308 default:
309 break;
310 }
311 }
312}
313
314
321{
322 uint8_t retval = PBV_MESSAGE_TX_ERROR;
323
324 if (uartActiveTx == true)
325 {
326 retval = PBV_MESSAGE_TX_ERROR;
327 }
328 else
329 {
331 {
332 case 0:
340 uartActiveTxAscii = true;
342 break;
343
344 case 1: //transfer header
345 if (PBV_ReadyToSend())// && (MsCounter_++ > 1)) //@ftx
346 {
348 PBV_Write(temp);
351 {
354 retval = PBV_MESSAGE_TRANSMITTING; // Why is this here?
355 }
356 }
357 else
358 {
359 retval = PBV_MESSAGE_TRANSMITTING;
360 }
361 break;
362
363 case 2: //transfer data
364 if (PBV_ReadyToSend())// && (MsCounter_++ > 1)) //@ftx
365 {
369 {
373 }
374 }
375 else
376 {
378 }
379 break;
380
381 case 3: //calculate CRC make end header
382 //IMPORTANT calculate crc before reusing PBV_HEADER
384 pbvUartObjectAscii.PBV_Header[1] = 0; //add crc
388 break;
389
390 case 4: //transfer header
391 if (PBV_ReadyToSend())
392 {
395 if (pbvUartObjectAscii.Offset >= 3)
396 {
399 uartActiveTxAscii = false;
401 }
402 }
403 else
404 {
406 }
407 break;
408
409 default:
411 break;
412 }
413 }
414 return retval;
415}
416
417
424{
425 uint8_t retval = PBV_MESSAGE_TX_ERROR;
426 if (uartActiveTxAscii == true)
427 {
428 retval = PBV_MESSAGE_TX_ERROR;
429 }
430 else
431 {
433 {
434 case 0:
442 uartActiveTx = true;
444 break;
445
446 case 1: //transfer header
447 if (PBV_ReadyToSend())// && (MsCounter_++ > 1)) //@ftx
448 {
450 PBV_Write(temp);
452 if (pbvUartObjectTx.Offset > 4)
453 {
457 }
458 }
459 else
460 {
462 }
463 break;
464
465 case 2: //transfer data
466 if (PBV_ReadyToSend())// && (MsCounter_++ > 1)) //@ftx
467 {
471 {
475 }
476 }
477 else
478 {
480 }
481 break;
482
483 case 3: //calculate CRC make end header
484 //IMPORTANT calculate crc before reusing PBV_HEADER
485 pbvUartObjectTx.PBV_Header[0] = 0; //
486 pbvUartObjectTx.PBV_Header[1] = 0; //add crc
490 break;
491
492 case 4: //transfer header
493 if (PBV_ReadyToSend())
494 {
497 if (pbvUartObjectTx.Offset >= 3)
498 {
501 uartActiveTx = false;
503 }
504 }
505 else
506 {
508 }
509 break;
510
511 default:
513 break;
514 }
515 }
516 return (retval);
517}
518
519
533
534
554
555
571
572
583uint16_t PBV_Calculate_CRC(uint8_t *message, uint16_t length)
584{
585 //CRC_Initialize();
586 uint8_t message_odd = (uint8_t) (length & 0x01);
587 uint16_t length_t = 0;
588 uint16_t i, j;
589 //uint16_t * message16bit;
590 uint32_t trycount = 0;
591 uint16_t resultCRC = 0;
592 uint16_t message16bit[PBV_RCV_DATABUFFER_SIZE];
593
594 //calCRCTEMP = 0; //
595
596 if (message_odd)
597 length_t = (length & 0xFFFE);
598 else
599 length_t = length;
600
601 for (i = 0, j = 0; i < length_t; i += 2, j++)
602 {
603 //*(message16bit + j) = (message[i + 1 ] << 8 ) | message[i] ;
604 message16bit[j] = (message[i + 1 ] << 8) | message[i];
605 }
606
607 CRC_CalculateBufferStart(message16bit, length_t);
608
609 while ((!CRC_CalculationIsDone()) && (trycount < 2000))
610 {
611 CRC_Tasks();
612 trycount++;
613 }
614
616
617 if (message_odd)
618 {
619 resultCRC ^= message[length - 1];
620 for (i = 0; i < 8; ++i)
621 {
622 if (resultCRC & 0x0001)
623 {
624 resultCRC = (resultCRC >> 1)^ PBV_CRC_POLYNOM_REV;
625 }
626 else
627 {
628 resultCRC = (resultCRC >> 1);
629 }
630 }
631 }
632 // clearing shift registers.
633 CRCWDATL = 0x0;
634 CRCWDATH = 0x0;
635
636 return resultCRC;
637}
This is the generated driver header file for the CRC driver.
This is the generated driver header file for the UART1 driver.
struct UART_MSG_TX_OBJ UART_MSG_TX_OBJ_t
UART msg Transmit object.
UART_MSG_TX_OBJ_t pbvUartObjectAscii
UART object for ascii tx.
Definition PBV_UART.c:154
void PBV_UART_Init(PBV_Datatype_TX_t *boardToPbv, PBV_Datatype_TX_t *boardToPbvAscii, PBV_Datatype_RX_t *pbvToBoard)
initializes UART objects
Definition PBV_UART.c:165
#define RCV_READ_LENGTH_HIGHBYTE
Definition PBV_UART.c:77
void PBV_UART_Reinit(PBV_Datatype_TX_t *ptr)
reinitializes the UART object with new protocol id.
Definition PBV_UART.c:526
uint16_t PBV_Calculate_CRC(uint8_t *message, uint16_t length)
Calculates the CRC on 16 bit stream of data.
Definition PBV_UART.c:583
#define RCV_READ_CRC_HIGHBYTE
Definition PBV_UART.c:80
struct UART_MSG_RX_OBJ UART_MSG_RX_OBJ_t
UART msg receive object.
#define PBV_ReadyToSend
linking functions with UART mcc driver functions
Definition PBV_UART.c:36
#define RCV_MESSAGE_RECEIVED
Definition PBV_UART.c:83
#define PBV_END_OF_FRAME
Standard End of frame for PBV UART frames.
Definition PBV_UART.c:66
#define PBV_RCV_DATABUFFER_SIZE
Maximum UART buffer. The max data that could be received is 64 bytes.
Definition PBV_UART.c:91
uint8_t PBV_UART_Transmit_to_GUI()
implements the state machine for UART numerical TX
Definition PBV_UART.c:423
UART_MSG_TX_OBJ_t pbvUartObjectTx
UART object for numeric tx.
Definition PBV_UART.c:142
#define RCV_READ_DATA
Definition PBV_UART.c:79
#define RCV_WAIT_FOR_STARTBYTE
Internal STATES for RX State machine for PBV UART Frames.
Definition PBV_UART.c:74
uint8_t PBV_UART_Transmit_Ascii_to_GUI()
implements the state machine for UART ascii TX
Definition PBV_UART.c:320
uint8_t PBV_UART_Receive_from_GUI()
implements the state machine for UART Frame receiving from PBV.
Definition PBV_UART.c:189
#define RCV_READ_LENGTH_LOWBYTE
Definition PBV_UART.c:78
#define PBV_HEADER_SIZE
Maximum UART buffer. The max data that could be received is 64 bytes.
Definition PBV_UART.c:98
void PBV_UART_Link_Data_TX(PBV_Datatype_TX_t *ptr)
links the data from the calling application to the UART TX object
Definition PBV_UART.c:541
int PBV_UART_Link_Data_RX(PBV_Datatype_RX_t *ptr)
Links the data from received frame to the calling application
Definition PBV_UART.c:562
#define RCV_READ_ID_HIGHBYTE
Definition PBV_UART.c:75
#define RCV_READ_EOF
Definition PBV_UART.c:82
#define PBV_CRC_POLYNOM_REV
Reversed polynomial. needed for odd byte in the data stream.
Definition PBV_UART.c:30
#define RCV_READ_CRC_LOWBYTE
Definition PBV_UART.c:81
#define PBV_START_OF_FRAME
Standard Start of frame for PBV UART frames.
Definition PBV_UART.c:60
#define PBV_Read
linking functions with UART mcc driver functions
Definition PBV_UART.c:54
#define RCV_READ_ID_LOWBYTE
Definition PBV_UART.c:76
#define PBV_Write
linking functions with UART mcc driver functions
Definition PBV_UART.c:42
#define PBV_IsRxReady
linking functions with UART mcc driver functions
Definition PBV_UART.c:48
UART_MSG_RX_OBJ_t pbvUartObjectRx
UART object for numeric rx.
Definition PBV_UART.c:148
@ PBV_MESSAGE_READY_TO_RECEIVE
Message Reception triggered.
@ PBV_STATE_RECEIVING
Message beinf received.
@ PBV_MESSAGE_RECEIVED
Message Received.
@ PBV_MESSAGE_RX_ERROR
if some error happens (CRC ?)
@ PBV_SIGNAL_MODE
Object will transmit/receive signals.
@ PBV_MESSAGE_TRANSMITTED
Message transmitted. successful transmit.
@ PBV_MESSAGE_TX_ERROR
if some error happens. unsuccessful transmit
@ PBV_MESSAGE_TRANSMITTING
Transmitting Message.
void CRC_CalculateBufferStart(void *buffer, uint32_t sizeBytes)
CRC module calculation on a buffer in data space.
Definition crc.c:373
void CRC_Tasks(void)
This function cycles through the CRC calculations. This function will load the CRC module FIFO with...
Definition crc.c:487
uint32_t CRC_CalculationResultReverseGet(void)
Gets the CRC reversed value of result if the calculation is done.
Definition crc.c:447
bool CRC_CalculationIsDone(void)
Returns the CRC calculation complete status
Definition crc.c:420
uint32_t PBV_Protcol_ID
Protocol ID.
uint8_t * Data_Buffer
Pointer to the data to be transmitted.
enum PBV_SIGNAL_ASCII_MODE PBV_Signal_Ascii
Object will transmit Signals or ASCII.
uint16_t Length
Length of transmitted message in Bytes.
uint32_t PBV_Protcol_ID
Protocol ID of the received message.
PBV_MESSAGE_RX_STATE_t PBV_Message_State
State of the received message.
uint8_t * Data_Buffer
Received Data pointer.
uint16_t Length
Length of received message in Bytes.
UART msg receive object.
Definition PBV_UART.c:105
uint8_t Length_in_Bytes
UART bytes Received
Definition PBV_UART.c:107
uint8_t PBV_Header[PBV_HEADER_SIZE]
recived Header. 5bytes ( SOF + ID + Length)
Definition PBV_UART.c:111
uint8_t UART_Frame_State
UART Receive Frame states.
Definition PBV_UART.c:106
uint16_t Protocol_ID
Received Frame ID.
Definition PBV_UART.c:108
uint16_t Offset
Offset needed to store data in data
Definition PBV_UART.c:109
uint8_t data[PBV_RCV_DATABUFFER_SIZE]
data buffer. linked by the calling application. STATIC(64) bytes for now.
Definition PBV_UART.c:112
uint16_t CRC
received CRC
Definition PBV_UART.c:110
UART msg Transmit object.
Definition PBV_UART.c:120
uint8_t Length_in_Bytes
UART Bytes to be Transmitted.
Definition PBV_UART.c:122
uint8_t PBV_Header[PBV_HEADER_SIZE]
sent Header. 5bytes ( SOF + ID + Length)
Definition PBV_UART.c:126
uint8_t UART_Frame_State
UART Transmit Frame states.
Definition PBV_UART.c:121
uint8_t * data
data buffer. linked by the calling application. STATIC(64) bytes for now.
Definition PBV_UART.c:127
uint16_t Protocol_ID
Frame ID to be appended.
Definition PBV_UART.c:123
uint16_t Offset
Offset needed for data transmission.
Definition PBV_UART.c:124
uint16_t CRC
calculated CRC
Definition PBV_UART.c:125