123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- #include "ZComDef.h"
- #include "OSAL.h"
- #include "hal_uart.h"
- #include "MT.h"
- #include "MT_UART.h"
- #include "OSAL_Memory.h"
- #define SOP_STATE 0x00
- #define CMD_STATE1 0x01
- #define CMD_STATE2 0x02
- #define LEN_STATE 0x03
- #define DATA_STATE 0x04
- #define FCS_STATE 0x05
- byte App_TaskID;
- uint8 state;
- uint8 CMD_Token[2];
- uint8 LEN_Token;
- uint8 FSC_Token;
- mtOSALSerialData_t *pMsg;
- uint8 tempDataLen;
- #if defined (ZAPP_P1) || defined (ZAPP_P2)
- uint16 MT_UartMaxZAppBufLen;
- bool MT_UartZAppRxStatus;
- #endif
- void MT_UartInit ()
- {
- halUARTCfg_t uartConfig;
-
- App_TaskID = 0;
-
- uartConfig.configured = TRUE;
- uartConfig.baudRate = MT_UART_DEFAULT_BAUDRATE;
- uartConfig.flowControl = MT_UART_DEFAULT_OVERFLOW;
- uartConfig.flowControlThreshold = MT_UART_DEFAULT_THRESHOLD;
- uartConfig.rx.maxBufSize = MT_UART_DEFAULT_MAX_RX_BUFF;
- uartConfig.tx.maxBufSize = MT_UART_DEFAULT_MAX_TX_BUFF;
- uartConfig.idleTimeout = MT_UART_DEFAULT_IDLE_TIMEOUT;
- uartConfig.intEnable = TRUE;
- #if defined (ZTOOL_P1) || defined (ZTOOL_P2)
- uartConfig.callBackFunc = MT_UartProcessZToolData;
- #elif defined (ZAPP_P1) || defined (ZAPP_P2)
- uartConfig.callBackFunc = MT_UartProcessZAppData;
- #else
- uartConfig.callBackFunc = NULL;
- #endif
-
- #if defined (MT_UART_DEFAULT_PORT)
- HalUARTOpen (MT_UART_DEFAULT_PORT, &uartConfig);
- #else
-
- (void)uartConfig;
- #endif
-
- #if defined (ZAPP_P1) || defined (ZAPP_P2)
-
- MT_UartMaxZAppBufLen = 1;
- MT_UartZAppRxStatus = MT_UART_ZAPP_RX_READY;
- #endif
- }
- void MT_UartRegisterTaskID( byte taskID )
- {
- App_TaskID = taskID;
- }
- byte MT_UartCalcFCS( uint8 *msg_ptr, uint8 len )
- {
- byte x;
- byte xorResult;
- xorResult = 0;
- for ( x = 0; x < len; x++, msg_ptr++ )
- xorResult = xorResult ^ *msg_ptr;
- return ( xorResult );
- }
- void MT_UartProcessZToolData ( uint8 port, uint8 event )
- {
- uint8 ch;
- uint8 bytesInRxBuffer;
-
- (void)event;
- while (Hal_UART_RxBufLen(port))
- {
- HalUARTRead (port, &ch, 1);
- switch (state)
- {
- case SOP_STATE:
- if (ch == MT_UART_SOF)
- state = LEN_STATE;
- break;
- case LEN_STATE:
- LEN_Token = ch;
- tempDataLen = 0;
-
- pMsg = (mtOSALSerialData_t *)osal_msg_allocate( sizeof ( mtOSALSerialData_t ) +
- MT_RPC_FRAME_HDR_SZ + LEN_Token );
- if (pMsg)
- {
-
- pMsg->hdr.event = CMD_SERIAL_MSG;
- pMsg->msg = (uint8*)(pMsg+1);
- pMsg->msg[MT_RPC_POS_LEN] = LEN_Token;
- state = CMD_STATE1;
- }
- else
- {
- state = SOP_STATE;
- return;
- }
- break;
- case CMD_STATE1:
- pMsg->msg[MT_RPC_POS_CMD0] = ch;
- state = CMD_STATE2;
- break;
- case CMD_STATE2:
- pMsg->msg[MT_RPC_POS_CMD1] = ch;
-
- if (LEN_Token)
- {
- state = DATA_STATE;
- }
- else
- {
- state = FCS_STATE;
- }
- break;
- case DATA_STATE:
-
- pMsg->msg[MT_RPC_FRAME_HDR_SZ + tempDataLen++] = ch;
-
- bytesInRxBuffer = Hal_UART_RxBufLen(port);
-
- if (bytesInRxBuffer <= LEN_Token - tempDataLen)
- {
- HalUARTRead (port, &pMsg->msg[MT_RPC_FRAME_HDR_SZ + tempDataLen], bytesInRxBuffer);
- tempDataLen += bytesInRxBuffer;
- }
- else
- {
- HalUARTRead (port, &pMsg->msg[MT_RPC_FRAME_HDR_SZ + tempDataLen], LEN_Token - tempDataLen);
- tempDataLen += (LEN_Token - tempDataLen);
- }
-
- if ( tempDataLen == LEN_Token )
- state = FCS_STATE;
- break;
- case FCS_STATE:
- FSC_Token = ch;
-
- if ((MT_UartCalcFCS ((uint8*)&pMsg->msg[0], MT_RPC_FRAME_HDR_SZ + LEN_Token) == FSC_Token))
- {
- osal_msg_send( App_TaskID, (byte *)pMsg );
- }
- else
- {
-
- osal_msg_deallocate ( (uint8 *)pMsg );
- }
-
- state = SOP_STATE;
- break;
- default:
- break;
- }
- }
- }
- #if defined (ZAPP_P1) || defined (ZAPP_P2)
- void MT_UartProcessZAppData ( uint8 port, uint8 event )
- {
- osal_event_hdr_t *msg_ptr;
- uint16 length = 0;
- uint16 rxBufLen = Hal_UART_RxBufLen(MT_UART_DEFAULT_PORT);
-
- if ((MT_UartMaxZAppBufLen != 0) && (MT_UartMaxZAppBufLen <= rxBufLen))
- {
- length = MT_UartMaxZAppBufLen;
- }
- else
- {
- length = rxBufLen;
- }
-
- if (event == HAL_UART_TX_FULL)
- {
-
- return;
- }
- if (event & ( HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT))
- {
- if ( App_TaskID )
- {
-
- if ((MT_UartZAppRxStatus == MT_UART_ZAPP_RX_READY ) && (length != 0))
- {
-
- MT_UartAppFlowControl (MT_UART_ZAPP_RX_NOT_READY);
-
- msg_ptr = (osal_event_hdr_t *)osal_msg_allocate( length + sizeof(osal_event_hdr_t) );
- if ( msg_ptr )
- {
- msg_ptr->event = SPI_INCOMING_ZAPP_DATA;
- msg_ptr->status = length;
-
- HalUARTRead( MT_UART_DEFAULT_PORT, (uint8 *)(msg_ptr + 1), length );
-
- osal_msg_send( App_TaskID, (uint8 *)msg_ptr );
- }
- }
- }
- }
- }
- void MT_UartZAppBufferLengthRegister ( uint16 maxLen )
- {
-
- if (maxLen <= MT_UART_DEFAULT_MAX_RX_BUFF)
- MT_UartMaxZAppBufLen = maxLen;
- else
- MT_UartMaxZAppBufLen = 1;
- }
- void MT_UartAppFlowControl ( bool status )
- {
-
- if (status != MT_UartZAppRxStatus )
- {
- MT_UartZAppRxStatus = status;
- }
-
- if (status == MT_UART_ZAPP_RX_READY)
- {
- MT_UartProcessZAppData (MT_UART_DEFAULT_PORT, HAL_UART_RX_TIMEOUT );
- }
- }
- #endif
|