|
@@ -0,0 +1,831 @@
|
|
|
+/************************************************************************
|
|
|
+ Project: Welling FAN Motor Developing platform
|
|
|
+ Filename: uart_monitor.c
|
|
|
+ Partner Filename: uart_monitor.h
|
|
|
+ Description: Partner file of uart_monitor.h
|
|
|
+ Complier: IAR Embedded Workbench for ARM 7.80, IAR Systems.
|
|
|
+ CPU TYPE : ST32F0xx
|
|
|
+*************************************************************************
|
|
|
+ Copyright (c) 2019 Welling Motor Technology(Shanghai) Co. Ltd.
|
|
|
+ All rights reserved.
|
|
|
+*************************************************************************
|
|
|
+*************************************************************************
|
|
|
+Revising History (ECL of this file):
|
|
|
+20191225, by cyf, create this file;
|
|
|
+************************************************************************/
|
|
|
+
|
|
|
+/************************************************************************
|
|
|
+ Beginning of File, do not put anything above here except notes
|
|
|
+ Compiler Directives:
|
|
|
+*************************************************************************/
|
|
|
+#ifndef _UART_MONITOR_C_
|
|
|
+ #define _UART_MONITOR_C_
|
|
|
+#endif
|
|
|
+/************************************************************************
|
|
|
+ Included File:
|
|
|
+*************************************************************************/
|
|
|
+#include "uart_monitor.h"
|
|
|
+#include "api.h"
|
|
|
+#include "board_config.h"
|
|
|
+
|
|
|
+/************************************************************************
|
|
|
+ Exported Functions:
|
|
|
+*************************************************************************/
|
|
|
+ULONG TempPara1=11;
|
|
|
+ULONG TempPara2=22;
|
|
|
+ULONG TempPara3=33;
|
|
|
+ULONG TempPara4=44;
|
|
|
+
|
|
|
+/*--------------------------------------------------------------------------------
|
|
|
+ Parameter group (R/W)
|
|
|
+--------------------------------------------------------------------------------*/
|
|
|
+UWORD* UART_pParaTable[PARA_TABLE_SIZE]=
|
|
|
+{
|
|
|
+ HIGH_WORD(dummy32) , // 000 P-00(Reserve) Never use!!
|
|
|
+ LOW_WORD(dummy32) , // 001 P-00(Reserve) Never use!!
|
|
|
+/*--------------------------------------------------------------------------------
|
|
|
+ 32-bits parameters (signed or unsigned)
|
|
|
+--------------------------------------------------------------------------------*/
|
|
|
+ //--------P01----------
|
|
|
+ HIGH_WORD(TempPara1) , // 002 P-01(high word)
|
|
|
+ LOW_WORD(TempPara1) , // 003 P-01(low word)
|
|
|
+ //--------P02----------
|
|
|
+ HIGH_WORD(TempPara2) , // 004 P-02(high word)
|
|
|
+ LOW_WORD(TempPara2) , // 005 P-02(low word)
|
|
|
+ //--------P03----------
|
|
|
+ HIGH_WORD(TempPara3) , // 006 P-03(high word)
|
|
|
+ LOW_WORD(TempPara3) , // 007 P-03(low word)
|
|
|
+ //--------P04----------
|
|
|
+ HIGH_WORD(TempPara4) , // 008 P-04(high word)
|
|
|
+ LOW_WORD(TempPara4) , // 009 P-04(low word)
|
|
|
+ //--------P05----------
|
|
|
+ HIGH_WORD(dummy32) , // 010 P-05(high word)
|
|
|
+ LOW_WORD(dummy32) , // 011 P-05(low word)
|
|
|
+ //--------P06----------
|
|
|
+ HIGH_WORD(dummy32) , // 012 P-06(high word)
|
|
|
+ LOW_WORD(dummy32) , // 013 P-06(low word)
|
|
|
+/*--------------------------------------------------------------------------------
|
|
|
+ 16-bits parameters (signed or unsigned)
|
|
|
+--------------------------------------------------------------------------------*/
|
|
|
+ //--------P07----------
|
|
|
+ &placeholder[0] , // 014 Don't change!
|
|
|
+ LOW_WORD(dummy32) , // 015 P-07(one word)
|
|
|
+ //--------P08----------
|
|
|
+ &placeholder[1] , // 016 Don't change!
|
|
|
+ LOW_WORD(dummy32) , // 017 P-08(one word)
|
|
|
+ //--------P09----------
|
|
|
+ &placeholder[2] , // 018 Don't change!
|
|
|
+ LOW_WORD(dummy32) , // 019 P-09(one word)
|
|
|
+ //--------P10----------
|
|
|
+ &placeholder[3] , // 020 Don't change!
|
|
|
+ LOW_WORD(dummy32) // 021 P-10(one word)
|
|
|
+};
|
|
|
+/***************************************************************
|
|
|
+ Function: uart_voMonitorInit
|
|
|
+ Description: Initialize hardware
|
|
|
+ Call by: hwsetup;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_voMonitorInit(void)
|
|
|
+{
|
|
|
+ UBYTE i = 0;
|
|
|
+ UART_fpError = (CB_ERR)CBError_monitor; // Store CallBack function pointers
|
|
|
+ uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer,uart_voCBDoneRead);
|
|
|
+ //--- insert stack initialization ---
|
|
|
+ for(i=0;i<MSG_QUEUE_SIZE;i++)
|
|
|
+ {
|
|
|
+ UART_stMsgQueue[i].stMsg.enType = UART_NULL;
|
|
|
+ if(i==(MSG_QUEUE_SIZE-1))
|
|
|
+ {
|
|
|
+ UART_stMsgQueue[i].pNext = &UART_stMsgQueue[0];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UART_stMsgQueue[i].pNext = &UART_stMsgQueue[i+1];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ UART_Config = UART_stConfigLEN;
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description:
|
|
|
+ Call by: main;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void CBDoneRead2(UART_ERR_Type _err, ULONG _NumBytes)
|
|
|
+{
|
|
|
+ switch(_err)
|
|
|
+ {
|
|
|
+ case UART_ERR_OK:
|
|
|
+ uart_DataNum2 = _NumBytes; // copy received data number
|
|
|
+ break;
|
|
|
+ case UART_ERR_TIMEOUT:
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2);//ly add
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description: Query flag ,then callback decode function
|
|
|
+ Call by: main;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_voMainDec(void)
|
|
|
+{
|
|
|
+ if(UART_uwRxNum)
|
|
|
+ {
|
|
|
+ uart_voDecode();
|
|
|
+ UART_uwRxNum = 0; // clear UART_uwRxNum for the next read !!
|
|
|
+ uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer,uart_voCBDoneRead);
|
|
|
+ }
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description: decode instruction
|
|
|
+ Call by: UART_voMainDec();
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_voDecode(void)
|
|
|
+{
|
|
|
+ UWORD i;
|
|
|
+ UBYTE temp_ubCheckCode;
|
|
|
+ ULONG temp_ulAddr[CHANNEL_NUM];
|
|
|
+ UART_MSG twmp_stMesg;
|
|
|
+ //---------------------- Serial-X application -----------------------------
|
|
|
+ if(UART_uwRxNum == UART_CMD_LENGTH)
|
|
|
+ {
|
|
|
+ temp_ubCheckCode = uart_ubCheckXOR(UART_ubReadBuffer,UART_CMD_LENGTH-1);
|
|
|
+ if(temp_ubCheckCode == UART_ubReadBuffer[UART_CMD_LENGTH-1])
|
|
|
+ {
|
|
|
+ //++++++++++++++++++++ decode monitor address ++++++++++++++++++++++++
|
|
|
+ if((UART_ubReadBuffer[0]==0x11)&&(UART_ubReadBuffer[1]==0x00))
|
|
|
+ {
|
|
|
+ if(UART_ubReadBuffer[UART_CMD_LENGTH-2])
|
|
|
+ {
|
|
|
+ UART_bMonSwitch = TRUE;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UART_bMonSwitch = FALSE;
|
|
|
+ }
|
|
|
+ if(UART_bMonSwitch)// *Application Specific*
|
|
|
+ {
|
|
|
+ temp_ulAddr[0] = 0x20000000 + ((ULONG)UART_ubReadBuffer[2] << 16) + ((ULONG)UART_ubReadBuffer[3] << 8) + ((ULONG)UART_ubReadBuffer[4] );
|
|
|
+ temp_ulAddr[1] = 0x20000000 + ((ULONG)UART_ubReadBuffer[5] << 16) + ((ULONG)UART_ubReadBuffer[6] << 8) + ((ULONG)UART_ubReadBuffer[7] );
|
|
|
+ temp_ulAddr[2] = 0x20000000 + ((ULONG)UART_ubReadBuffer[8] << 16) + ((ULONG)UART_ubReadBuffer[9] << 8) + ((ULONG)UART_ubReadBuffer[10]);
|
|
|
+ temp_ulAddr[3] = 0x20000000 + ((ULONG)UART_ubReadBuffer[11] << 16) + ((ULONG)UART_ubReadBuffer[12] << 8) + ((ULONG)UART_ubReadBuffer[13]);
|
|
|
+ temp_ulAddr[4] = 0x20000000 + ((ULONG)UART_ubReadBuffer[14] << 16) + ((ULONG)UART_ubReadBuffer[15] << 8) + ((ULONG)UART_ubReadBuffer[16]);
|
|
|
+ temp_ulAddr[5] = 0x20000000 + ((ULONG)UART_ubReadBuffer[17] << 16) + ((ULONG)UART_ubReadBuffer[18] << 8) + ((ULONG)UART_ubReadBuffer[19]);
|
|
|
+ /* handle address */
|
|
|
+ for(i=0; i<CHANNEL_NUM; i++)// *Application Specific*
|
|
|
+ {
|
|
|
+ if(temp_ulAddr[i] >= 0x20000000 && temp_ulAddr[i] <= HW_RAM_END_ADDRESS) // STM32F03x SRAM range
|
|
|
+ {
|
|
|
+ UART_pMonAddr[i] = (unsigned short *)(temp_ulAddr[i]&0xfffffffe);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UART_uwWriteIndex = 4; // restore initial WriterBufferIndex
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //++++++++++++++++++++ decode R/W Parameter command ++++++++++++++++++++++++
|
|
|
+ if((UART_ubReadBuffer[0]==0x11)&&(UART_ubReadBuffer[1]==0x10))
|
|
|
+ {
|
|
|
+ uart_voHandleParaCmd();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //++++++++++++++++++++ decode drying machine command ++++++++++++++++++++++++
|
|
|
+ if((UART_ubReadBuffer[0]==0x02)&&(UART_ubReadBuffer[1]==0x01))
|
|
|
+ {
|
|
|
+ if(UART_ubReadBuffer[2] == 0x11) // query command
|
|
|
+ {
|
|
|
+ twmp_stMesg.enType = UART_SEND_STATUS;
|
|
|
+ twmp_stMesg.pBuf = NULL;
|
|
|
+ twmp_stMesg.uwNumBytes =0;
|
|
|
+ uart_voPostMsg(twmp_stMesg);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description: callback called when error occurs
|
|
|
+ Call by: ;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void CBError_monitor(UART_ERR_Type _err)
|
|
|
+{
|
|
|
+ // switch (_err)
|
|
|
+ // {
|
|
|
+ // case UART_ERR_TIMEOUT:
|
|
|
+ // DMA1_Channel3->CCR &= (uint16_t)(~DMA_CCR_EN);
|
|
|
+ // DMA1->IFCR = DMA_ISR_GIF3;
|
|
|
+ // DMA1_Channel3->CNDTR = 22;
|
|
|
+ // DMA1_Channel3->CCR |= DMA_CCR_EN;
|
|
|
+ // break;
|
|
|
+ // case UART_ERR_NO_BUFFER:
|
|
|
+ // break;
|
|
|
+ // default:
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description: Callback called when some data received.
|
|
|
+ Call by: ;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_voCBDoneRead(UART_ERR_Type _err, ULONG _NumBytes)
|
|
|
+{
|
|
|
+ switch(_err)
|
|
|
+ {
|
|
|
+ case UART_ERR_OK:
|
|
|
+ UART_uwRxNum = _NumBytes; // copy received data number
|
|
|
+ break;
|
|
|
+ case UART_ERR_TIMEOUT:
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description: used to send monitor data out
|
|
|
+ Call by: tbc_voIsr();
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_voAppMonitor(void)
|
|
|
+{
|
|
|
+ if(UART_bMonSwitch)
|
|
|
+ {
|
|
|
+ UBYTE temp_ubBuf[TEMP_BUFFER_LEN];
|
|
|
+ UBYTE i;
|
|
|
+ //------ load monitor channels data (general) --------//
|
|
|
+ for(i=0;i<(TEMP_BUFFER_LEN>>1);i++) // less than half of TEMP_BUFFER_LEN
|
|
|
+ {
|
|
|
+ temp_ubBuf[i*2] = (*UART_pMonAddr[i])>>8; //Data.hi
|
|
|
+ temp_ubBuf[i*2+1] = (*UART_pMonAddr[i])&0xff; //Data.lo
|
|
|
+ }
|
|
|
+ //------ loading data from tempBuffer to Writebuffer and send out when Writebuffer is full -------//
|
|
|
+ for(i = 0; i < TEMP_BUFFER_LEN; i++)
|
|
|
+ {
|
|
|
+ UART_pWriteBuffer[UART_uwWriteIndex] = temp_ubBuf[i]; // write latest data//data adrress
|
|
|
+ if(UART_uwWriteIndex == (S_FRAME_SIZE-1)) //if full, start Write_Async
|
|
|
+ {
|
|
|
+ if(!UART_stParaStatus.bWriteBusy)
|
|
|
+ {
|
|
|
+ UART_stParaStatus.bWriteBusy = TRUE;
|
|
|
+ if((UART_bInsertPendTx==TRUE)&&(UART_pWriteBuffer == UART_ubWriteBuffer2)) // send writeBuffer and insertBuffer
|
|
|
+ {
|
|
|
+ UART_stParaStatus.bParaStart = TRUE;
|
|
|
+ iUart_WriteAsync(0, UART_pWriteBuffer, S_FRAME_SIZE+16, uart_TxCompleteCallback, uart_TxErrorCallback);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UART_stParaStatus.bParaStart = FALSE;
|
|
|
+ iUart_WriteAsync(0, UART_pWriteBuffer, S_FRAME_SIZE, uart_TxCompleteCallback, uart_TxErrorCallback);
|
|
|
+ }
|
|
|
+ if(UART_pWriteBuffer == UART_ubWriteBuffer1)
|
|
|
+ {
|
|
|
+ UART_pWriteBuffer = UART_ubWriteBuffer2;
|
|
|
+ } //dual buffer swap.
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UART_pWriteBuffer = UART_ubWriteBuffer1;
|
|
|
+ }
|
|
|
+ //initialize UART_uwWriteIndex
|
|
|
+ UART_uwWriteIndex = 4; // utilize later 60bytes of 64bytes of write-frame
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UART_uwWriteIndex = 4;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UART_uwWriteIndex++;
|
|
|
+ } //index increase to next write position
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description: post message
|
|
|
+ Call by: main;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_voPostMsg(UART_MSG _msg)
|
|
|
+{
|
|
|
+ if((UART_stMsgQ.pSave == UART_stMsgQ.pUse)&&(UART_stMsgQ.bValid == TRUE))
|
|
|
+ {
|
|
|
+ return;// if UART_stMsgQueue is full, just return
|
|
|
+ }
|
|
|
+ UART_stMsgQ.pSave->stMsg = _msg;
|
|
|
+ UART_stMsgQ.pSave = UART_stMsgQ.pSave->pNext;
|
|
|
+ UART_stMsgQ.bValid = TRUE;
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description: handle parameter related command
|
|
|
+ Call by: UART_voDecode;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_voHandleParaCmd(void)
|
|
|
+{
|
|
|
+ UWORD i;
|
|
|
+ UWORD temp_uwIndex;
|
|
|
+ UWORD temp_HiWord,temp_LowWord;
|
|
|
+ UART_MSG temp_stMsg;
|
|
|
+ if(UART_ubReadBuffer[2] == 0x01) // Read parameters command
|
|
|
+ {
|
|
|
+ for(i=2;i<PARA_TABLE_SIZE;i+=2)
|
|
|
+ {
|
|
|
+ //----- check 16bits data's sign ------
|
|
|
+ if(i>=14)
|
|
|
+ {
|
|
|
+ if(*UART_pParaTable[i+1]&0x8000)
|
|
|
+ {
|
|
|
+ *UART_pParaTable[i] = 0xFFFF;
|
|
|
+ } // if data16<0
|
|
|
+ else
|
|
|
+ {
|
|
|
+ *UART_pParaTable[i] = 0x0000;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // ----- load value (little-endian)-----
|
|
|
+ UART_ubParaBuffer[i*2-4] = (UBYTE)((*UART_pParaTable[i])>>8);
|
|
|
+ UART_ubParaBuffer[i*2-3] = (UBYTE)((*UART_pParaTable[i])&0x00FF);
|
|
|
+ UART_ubParaBuffer[i*2-2] = (UBYTE)((*UART_pParaTable[i+1])>>8);
|
|
|
+ UART_ubParaBuffer[i*2-1] = (UBYTE)((*UART_pParaTable[i+1])&0x00FF);
|
|
|
+ }
|
|
|
+ temp_stMsg.enType = UART_SEND_PARA;
|
|
|
+ temp_stMsg.pBuf = UART_ubParaBuffer;
|
|
|
+ temp_stMsg.uwNumBytes = PARA_BUFFER_SIZE ;
|
|
|
+ uart_voPostMsg(temp_stMsg); // post message
|
|
|
+ }
|
|
|
+ if(UART_ubReadBuffer[2] == 0x02) // Write Parameters command
|
|
|
+ {
|
|
|
+ temp_uwIndex = UART_ubReadBuffer[3];
|
|
|
+ temp_HiWord = ((UWORD)UART_ubReadBuffer[4]<<8) + ((UWORD)UART_ubReadBuffer[5]);
|
|
|
+ temp_LowWord = ((UWORD)UART_ubReadBuffer[6]<<8) +((UWORD)UART_ubReadBuffer[7]);
|
|
|
+ *UART_pParaTable[temp_uwIndex] = temp_HiWord;
|
|
|
+ *UART_pParaTable[temp_uwIndex+1] = temp_LowWord;
|
|
|
+ }
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description: Query if there is a message in msgQueue
|
|
|
+ Call by: SysTick_Handler;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_voApplTimer(void)
|
|
|
+{
|
|
|
+ UBYTE i = 0;
|
|
|
+ if(UART_stMsgQ.bValid) // if there is a message in msgQueue
|
|
|
+ {
|
|
|
+ //------------- fetch message ---------------//
|
|
|
+ if(UART_stMsgQ.bHandling == FALSE) // if there isn't any MSG under handling.
|
|
|
+ {
|
|
|
+ if(UART_stMsgQ.pUse != UART_stMsgQ.pSave)
|
|
|
+ {
|
|
|
+ UART_stMsgFetched = UART_stMsgQ.pUse->stMsg;// fetch message
|
|
|
+ UART_stMsgQ.pUse = UART_stMsgQ.pUse->pNext; // shift to next UART_stMsgQueue position
|
|
|
+ UART_stMsgQ.bHandling = TRUE; //set bMsgHandling flag, prevent from fetching new msg from UART_stMsgQueue.
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UART_stMsgQ.bValid = FALSE; // invalid UART_stMsgQueue(all message has been handled, prevent from calling UART_voHandleMsg again)
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //--------------------------------------------
|
|
|
+ if(UART_bInsertPendTx)
|
|
|
+ {
|
|
|
+ return;// if insertBuffer is pending to be transmitted, just return to wait
|
|
|
+ }
|
|
|
+ //------------ process message --------------//
|
|
|
+ switch(UART_stMsgFetched.enType)
|
|
|
+ {
|
|
|
+ case UART_NULL: // invalid message
|
|
|
+ break;
|
|
|
+ case UART_SEND_PARA:
|
|
|
+ {
|
|
|
+ if((UART_stMsgFetched.uwNumBytes==0)||(UART_stMsgFetched.pBuf==NULL))
|
|
|
+ {
|
|
|
+ UART_stMsgQ.bHandling = FALSE; // fetched message has been processed over(invalid message)
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ UART_ubInsertBuf[4] = 0x11; // parameter response frame header
|
|
|
+ UART_ubInsertBuf[5] = 0x10;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case UART_SEND_STATUS:
|
|
|
+ {
|
|
|
+ if((UART_stMsgFetched.uwNumBytes==0)||(UART_stMsgFetched.pBuf==NULL))
|
|
|
+ {
|
|
|
+ UART_stMsgQ.bHandling = FALSE; // fetched message has been processed over(invalid message)
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ UART_ubInsertBuf[4]=0x02; // application status response header
|
|
|
+ UART_ubInsertBuf[5]=0x01;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if(UART_stMsgFetched.uwNumBytes > (INSERT_BUFFER_SIZE-INSERT_BUFFER_HEADER_LEN))
|
|
|
+ {
|
|
|
+ UART_ubInsertBuf[INSERT_BUFFER_HEADER_LEN-1] = 0xFF;
|
|
|
+ UART_stMsgFetched.uwNumBytes -= (INSERT_BUFFER_SIZE-INSERT_BUFFER_HEADER_LEN);// bytes left to be sent out in modbus response.
|
|
|
+ for(i=0;i<(INSERT_BUFFER_SIZE-INSERT_BUFFER_HEADER_LEN);i++)
|
|
|
+ {
|
|
|
+ UART_ubInsertBuf[INSERT_BUFFER_HEADER_LEN+i] = *UART_stMsgFetched.pBuf;
|
|
|
+ UART_stMsgFetched.pBuf++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UART_ubInsertBuf[INSERT_BUFFER_HEADER_LEN-1] = UART_stMsgFetched.uwNumBytes;
|
|
|
+ for(i=0;i<UART_stMsgFetched.uwNumBytes;i++)
|
|
|
+ {
|
|
|
+ UART_ubInsertBuf[INSERT_BUFFER_HEADER_LEN+i] = *UART_stMsgFetched.pBuf;
|
|
|
+ UART_stMsgFetched.pBuf++;
|
|
|
+ }
|
|
|
+ UART_stMsgQ.bHandling = FALSE;// fetched message has been processed over(new message can be fetched)
|
|
|
+ }
|
|
|
+ UART_bInsertPendTx = TRUE; // insertBuffer is pending to be transmitted
|
|
|
+ //----------- transmit message -------------//
|
|
|
+ if(UART_bMonSwitch)//--- Load data in insertBuffer to WriteBuffer2 ---
|
|
|
+ {
|
|
|
+ for(i=0;i<INSERT_BUFFER_SIZE;i++)
|
|
|
+ {
|
|
|
+ UART_ubWriteBuffer2[S_FRAME_SIZE+i] = UART_ubInsertBuf[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UART_stParaStatus.bParaStart = TRUE;
|
|
|
+ iUart_WriteAsync(0, UART_ubInsertBuf, 16, uart_TxCompleteCallback, uart_TxErrorCallback);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description: XOR check when receive data.
|
|
|
+ Call by: main;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+UBYTE uart_ubCheckXOR(UBYTE* Buf, UBYTE CNT)
|
|
|
+{
|
|
|
+ UWORD temp_Xor = 0;
|
|
|
+ UWORD i;
|
|
|
+ for(i=0;i<CNT; i++)
|
|
|
+ {
|
|
|
+ temp_Xor ^= Buf[i];
|
|
|
+ }
|
|
|
+ return temp_Xor;
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description: Reconfigure when the data received incomletely in 1s
|
|
|
+ Call by: SysTick_Handler;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_voDriTimer(void)
|
|
|
+{
|
|
|
+ // if(DMA1_Channel3->CNDTR != 22)
|
|
|
+ // {
|
|
|
+ // ++UART_uwTimeoutCtr;
|
|
|
+ // if(UART_uwTimeoutCtr == UART_Config.uwTimeout) // Timeout match
|
|
|
+ // {
|
|
|
+ // UART_uwTimeoutCtr = 0;
|
|
|
+ // UART_fpError(UART_ERR_TIMEOUT);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // UART_uwTimeoutCtr = 0;
|
|
|
+ // }
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function:
|
|
|
+ Description:
|
|
|
+ Call by:
|
|
|
+ Input Variables:
|
|
|
+ Output/Return Variables:
|
|
|
+ Subroutine Call:
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_Read_Poll2(ULONG _NumBytes,UBYTE* _Buffer,CB_DONE_READ _fpCBDone)
|
|
|
+{
|
|
|
+ UBYTE i;
|
|
|
+ UBYTE* pBuffer = _Buffer;
|
|
|
+ //---clear receive data buffer!---
|
|
|
+ for(i=0;i<_NumBytes;i++)
|
|
|
+ {
|
|
|
+ *pBuffer = 0;
|
|
|
+ pBuffer++;
|
|
|
+ }
|
|
|
+ //---Set busy flag---
|
|
|
+ USART_Read.m_ReadBusy = TRUE; // clear ReadBusy flag
|
|
|
+ //---setup Read object---
|
|
|
+ USART_Read.m_BuffSize = _NumBytes;
|
|
|
+ USART_Read.m_DataBuff.pBuf = _Buffer;
|
|
|
+ USART_Read.m_fpDone = _fpCBDone;
|
|
|
+ USART_Read.m_DataBuff.NumBytes = 0;
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function:
|
|
|
+ Description:
|
|
|
+ Call by:
|
|
|
+ Input Variables:
|
|
|
+ Output/Return Variables:
|
|
|
+ Subroutine Call:
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void ReadFrame_Poll2(void)
|
|
|
+{
|
|
|
+ UBYTE tempRxData;
|
|
|
+ CB_DONE_READ CBTemp;
|
|
|
+ if(NULL == USART_Read.m_DataBuff.pBuf)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ while (iUart_Available(0))
|
|
|
+ {
|
|
|
+ iUart_Read(0, &tempRxData, 1); // save register data
|
|
|
+ UART_blCommErrFlag = FALSE;
|
|
|
+ if(uart_frameStartFlag == FALSE) // if the frame header has not been detected.
|
|
|
+ {
|
|
|
+ if(tempRxData != 0x11)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ } // 0xB2 is LittleSwan CMD frame header byte.
|
|
|
+ else // frame header detected
|
|
|
+ {
|
|
|
+ if(USART_Read.m_DataBuff.NumBytes==0) // ready to save data
|
|
|
+ {
|
|
|
+ uart_frameStartFlag = TRUE;
|
|
|
+ uart_receivedNum = 0;
|
|
|
+ }
|
|
|
+ else // not ready to save data
|
|
|
+ {
|
|
|
+ return; // just return (wait until ready)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //------------------------ cmd frameheader 0xB2 has been detected -----------------------
|
|
|
+ uart_receivedNum++;
|
|
|
+ uart_frameLength = 22;
|
|
|
+ if(uart_receivedNum == uart_frameLength)
|
|
|
+ {
|
|
|
+ uart_receivedNum =0;
|
|
|
+ uart_frameStartFlag = FALSE;
|
|
|
+ }
|
|
|
+ if(UART_blCommErrFlag == FALSE)
|
|
|
+ {
|
|
|
+ if(USART_Read.m_BuffSize > USART_Read.m_DataBuff.NumBytes)
|
|
|
+ {
|
|
|
+ if(USART_Read.m_DataBuff.pBuf != NULL)
|
|
|
+ {
|
|
|
+ *USART_Read.m_DataBuff.pBuf = tempRxData;
|
|
|
+ USART_Read.m_DataBuff.pBuf++;
|
|
|
+ USART_Read.m_DataBuff.NumBytes++;
|
|
|
+ }
|
|
|
+ if(USART_Read.m_DataBuff.NumBytes == uart_frameLength)
|
|
|
+ {
|
|
|
+ USART_Read.m_DataBuff.pBuf = NULL;
|
|
|
+ USART_Read.m_BuffSize = 0;
|
|
|
+ CBTemp = USART_Read.m_fpDone;
|
|
|
+ USART_Read.m_fpDone = NULL;
|
|
|
+ USART_Read.m_ReadBusy = FALSE; // clear ReadBusy flag !
|
|
|
+ if(CBTemp != NULL)
|
|
|
+ {
|
|
|
+ CBTemp(UART_ERR_OK, uart_frameLength);
|
|
|
+ }
|
|
|
+ uart_frameLength = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/***************************************************************
|
|
|
+ Function:
|
|
|
+ Description:
|
|
|
+ Call by: main;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void uart_voSwanInit(void)
|
|
|
+{
|
|
|
+ UART_fpError = (CB_ERR)CBError_Swan;
|
|
|
+ UART_Config = UART_stConfigLEN;
|
|
|
+ uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2); // use poll method for littleswan receiving.
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description:
|
|
|
+ Call by: main;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void CBError_Swan(UART_ERR_Type _err)
|
|
|
+{
|
|
|
+ switch (_err)
|
|
|
+ {
|
|
|
+ case UART_ERR_TIMEOUT:
|
|
|
+ uart_DataNum2 = 0; // clear s_DataNum2 for the next read !!
|
|
|
+ uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2);
|
|
|
+ break;
|
|
|
+ case UART_ERR_NO_BUFFER:
|
|
|
+ break;
|
|
|
+ case UART_ERR_UNKNOWN_CMD:
|
|
|
+ uart_DataNum2 = 0; // clear s_DataNum2 for the next read !!
|
|
|
+ uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function:
|
|
|
+ Description:
|
|
|
+ Call by:
|
|
|
+ Input Variables:
|
|
|
+ Output/Return Variables:
|
|
|
+ Subroutine Call:
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+//void WriteFrame_Poll2(void)
|
|
|
+//{
|
|
|
+// BOOL WriteOverFlag = FALSE;
|
|
|
+// if(NULL== USART_Write.m_DataBuff.pBuf)
|
|
|
+// {
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// if(USART_Write.m_DataBuff.NumBytes != 0)
|
|
|
+// {
|
|
|
+// USART1->TDR = *USART_Write.m_DataBuff.pBuf;
|
|
|
+// USART_Write.m_DataBuff.pBuf++;
|
|
|
+// USART_Write.m_DataBuff.NumBytes--;
|
|
|
+// if(0 == USART_Write.m_DataBuff.NumBytes)
|
|
|
+// {
|
|
|
+// WriteOverFlag = TRUE;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// if(TRUE == WriteOverFlag)
|
|
|
+// {
|
|
|
+// //disable TXEIE(bit7)
|
|
|
+// USART1->CR1 &= ~0x00000080;
|
|
|
+// WriteOverFlag = FALSE;
|
|
|
+// USART_Write.m_DataBuff.pBuf =NULL;
|
|
|
+// USART_Write.m_WriteBusy = FALSE;
|
|
|
+// }
|
|
|
+//}
|
|
|
+/***************************************************************
|
|
|
+ Function:
|
|
|
+ Description:
|
|
|
+ Call by:
|
|
|
+ Input Variables:
|
|
|
+ Output/Return Variables:
|
|
|
+ Subroutine Call:
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+void HAL_CancelRead_Poll2(void)
|
|
|
+{
|
|
|
+ USART_Read.m_BuffSize = 0;
|
|
|
+ uart_receivedNum =0;
|
|
|
+ uart_frameLength =0;
|
|
|
+ uart_frameType = 0x00;
|
|
|
+ uart_frameStartFlag = FALSE;
|
|
|
+ UART_fpError(UART_ERR_TIMEOUT);
|
|
|
+}
|
|
|
+/***************************************************************
|
|
|
+ Function
|
|
|
+ Description:
|
|
|
+ Call by: main;
|
|
|
+ Input Variables: N/A
|
|
|
+ Output/Return Variables: N/A
|
|
|
+ Subroutine Call: ...;
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+// void uart_voApplMain(void)
|
|
|
+// {
|
|
|
+// UBYTE* pBuffer = NULL;
|
|
|
+// UWORD numBytes = 0;
|
|
|
+// if(uart_DataNum2)
|
|
|
+// {
|
|
|
+// if(SwanDecoder(uart_pReadBuffer2, uart_DataNum2, &pBuffer, &numBytes))
|
|
|
+// {
|
|
|
+// uart_Write_Poll2(numBytes, pBuffer);
|
|
|
+// }
|
|
|
+// uart_DataNum2 = 0;
|
|
|
+// uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2);
|
|
|
+// }
|
|
|
+// }
|
|
|
+/***************************************************************
|
|
|
+ Function:
|
|
|
+ Description:
|
|
|
+ Call by:
|
|
|
+ Input Variables:
|
|
|
+ Output/Return Variables:
|
|
|
+ Subroutine Call:
|
|
|
+ Reference: N/A
|
|
|
+****************************************************************/
|
|
|
+// void uart_Write_Poll2(ULONG _NumBytes,const UBYTE* _Buffer)
|
|
|
+// {
|
|
|
+// if(USART_Write.m_WriteBusy == TRUE)
|
|
|
+// {
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// USART_Write.m_WriteBusy = TRUE;
|
|
|
+// USART_Write.m_DataBuff.pBuf = (UBYTE*)_Buffer;
|
|
|
+// USART_Write.m_DataBuff.NumBytes = _NumBytes;
|
|
|
+// //enable TXEIE(bit7)
|
|
|
+// USART1->CR1 |= 0x00000080;
|
|
|
+// }
|
|
|
+
|
|
|
+void uart_TxCompleteCallback()
|
|
|
+{
|
|
|
+ if( UART_stParaStatus.bParaStart)
|
|
|
+ {
|
|
|
+ UART_bInsertPendTx = FALSE; // clear insertBuffer pending
|
|
|
+ UART_stParaStatus.bParaStart = FALSE; //clear parameter status
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //do nothing
|
|
|
+ }
|
|
|
+ UART_stParaStatus.bWriteBusy = FALSE;
|
|
|
+}
|
|
|
+
|
|
|
+void uart_TxErrorCallback()
|
|
|
+{
|
|
|
+ if( UART_stParaStatus.bParaStart)
|
|
|
+ {
|
|
|
+ UART_bInsertPendTx = FALSE; // clear insertBuffer pending
|
|
|
+ UART_stParaStatus.bParaStart = FALSE; //clear parameter status
|
|
|
+ }
|
|
|
+ UART_stParaStatus.bWriteBusy = FALSE;
|
|
|
+}
|
|
|
+/************************************************************************
|
|
|
+ Local Functions:
|
|
|
+*************************************************************************/
|
|
|
+
|
|
|
+#ifdef _UART_MONITOR_C_
|
|
|
+ #undef _UART_MONITOR_C_
|
|
|
+#endif
|
|
|
+/************************************************************************
|
|
|
+ Copyright (c) 2019 Welling Motor Technology(Shanghai) Co. Ltd.
|
|
|
+ All rights reserved.
|
|
|
+*************************************************************************
|
|
|
+ End of this File (EOF)!
|
|
|
+ Do not put anything after this part!
|
|
|
+*************************************************************************/
|
|
|
+
|