/************************************************************************ 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 #define FALSE (0U) #define TRUE (1U) /************************************************************************ Included File: *************************************************************************/ #include "uart_monitor.h" //#include "api.h" #include "board_config.h" #include "spdctrFSM.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(TempPara4) , // 002 P-01(high word) LOW_WORD(TempPara4) , // 003 P-01(low word) //--------P02---------- HIGH_WORD(TempPara4) , // 004 P-02(high word) LOW_WORD(TempPara4) , // 005 P-02(low word) //--------P03---------- HIGH_WORD(TempPara4) , // 006 P-03(high word) LOW_WORD(TempPara4) , // 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_ERROR)CBError_monitor; // Store CallBack function pointers uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer,uart_voCBDoneRead); //--- insert stack initialization --- for(i=0;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=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;iCNDTR != 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_ERROR)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; case UART_ERR_OK: 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; } /*************************************************************** Function: ;crc_ccitt(polynomial:1021) Description: Call by: Input Variables: N/A Output/Return Variables: N/A Subroutine Call: ...; Reference: N/A ****************************************************************/ #ifdef CRC_CCITT UWORD uart_uwCRCCcitt(UBYTE *ptubBuf, UWORD length) { UBYTE b = 0; UWORD crc = 0xffff; UWORD i, j; for (i = 0; i < length; i++) { for (j = 0; j < 8; j++) { b = ((ptubBuf[i] << j) & 0x80) ^ ((crc & 0x8000) >> 8); crc <<= 1; if (b != 0) { crc ^= 0x1021; } } } return crc; } #endif /************************************************************************ 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! *************************************************************************/