665 lines
18 KiB
C
665 lines
18 KiB
C
|
|
/**
|
|||
|
|
******************************************************************************
|
|||
|
|
* @file TMC_UART.c
|
|||
|
|
* @author TMC Terminal Team
|
|||
|
|
* @version V1.0.0
|
|||
|
|
* @date 06/21/2018
|
|||
|
|
* @brief UART TMC module driver.
|
|||
|
|
* This file provides functions for UART.
|
|||
|
|
*
|
|||
|
|
******************************************************************************
|
|||
|
|
*
|
|||
|
|
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
|||
|
|
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
|
|||
|
|
* TIME. AS A RESULT, TMC SHALL NOT BE HELD LIABLE FOR ANY
|
|||
|
|
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
|
|||
|
|
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
|
|||
|
|
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
|||
|
|
*
|
|||
|
|
* <h2><center>© COPYRIGHT 2016 TMC</center></h2>
|
|||
|
|
******************************************************************************/
|
|||
|
|
/* Includes ------------------------------------------------------------------*/
|
|||
|
|
|
|||
|
|
#include "global.h"
|
|||
|
|
|
|||
|
|
static TMC_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
|
|||
|
|
|
|||
|
|
Uart_Receive_PROTOCOL gUartFlag;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @function BccCheck
|
|||
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD>
|
|||
|
|
* @param[in] pSrc ָ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @param[in] uLen <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @return У<EFBFBD><EFBFBD>ֵ
|
|||
|
|
*/
|
|||
|
|
uint8_t BccCheck(uint8_t *pSrc, uint16_t uLen)
|
|||
|
|
{
|
|||
|
|
uint32_t i = 0;
|
|||
|
|
uint8_t uBcc = 0;
|
|||
|
|
|
|||
|
|
for(i = 0; i < uLen; i++)
|
|||
|
|
{
|
|||
|
|
uBcc ^= pSrc[i];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return uBcc;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @function AppUartInit
|
|||
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ڳ<EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
|
* @param[in] baudRate <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @param[in] parityMode У<EFBFBD><EFBFBD>ģʽ
|
|||
|
|
* @return <EFBFBD><EFBFBD>
|
|||
|
|
*/
|
|||
|
|
void AppUartInit(uint32_t baudRate, uint32_t parityMode)
|
|||
|
|
{
|
|||
|
|
ghuart1.UARTx = UART1;
|
|||
|
|
TMC_UART_DeInit(&ghuart1);
|
|||
|
|
UART_StructInit(&ghuart1.Init);
|
|||
|
|
ghuart1.Init.BaudRate = baudRate;
|
|||
|
|
ghuart1.Init.ParityMode = parityMode;
|
|||
|
|
TMC_UART_Init(&ghuart1);
|
|||
|
|
gUartFlag = UART_PROTOCOL_STX;
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>жϽ<D0B6><CFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷŵ<DDB7>gUartBuff
|
|||
|
|
SET_UART_Receive_IT(&ghuart1, gUartBuff, 1);
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* @function AppUartSendData
|
|||
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @param[in] buf <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buf
|
|||
|
|
* @param[in] len <EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @return TMC_OK/TMC_ERROR/TMC_TIMEOUT/TMC_BUSY
|
|||
|
|
*/
|
|||
|
|
uint8_t AppUartSendDataComm(uint8_t *buf, uint32_t len)
|
|||
|
|
{
|
|||
|
|
uint8_t *pUARTbuf, *pOff;
|
|||
|
|
uint8_t status;
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>FID<49><44>֯<EFBFBD><D6AF><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD>ʽΪ 5A 01 [len] [data] Bcc A5
|
|||
|
|
pUARTbuf = pOff = (uint8_t *) POSOUTBUF;
|
|||
|
|
*pOff++ = UART_PROTOCOL_STX_VALUE;
|
|||
|
|
*pOff++ = UART_PROTOCOL_RESPONSE_FID_VALUE;
|
|||
|
|
*pOff++ = (uint8_t) (len >> 8);
|
|||
|
|
*pOff++ = (uint8_t)(len);
|
|||
|
|
memcpy(pOff, buf, len);
|
|||
|
|
pOff += len;
|
|||
|
|
*pOff++ = BccCheck(pUARTbuf + 1, len + 3);
|
|||
|
|
*pOff++ = UART_PROTOCOL_ETX_VALUE;
|
|||
|
|
NVIC_DisableIRQ(UART1_IRQn);
|
|||
|
|
status = TMC_UART_Transmit(&ghuart1, pUARTbuf, len + 6);
|
|||
|
|
NVIC_EnableIRQ(UART1_IRQn);
|
|||
|
|
TimerDelay(TIM2, 1);
|
|||
|
|
return status;
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* @function AppUartSendData
|
|||
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @param[in] buf <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buf
|
|||
|
|
* @param[in] len <EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @return TMC_OK/TMC_ERROR/TMC_TIMEOUT/TMC_BUSY
|
|||
|
|
*/
|
|||
|
|
uint8_t AppUartSendData(uint8_t *buf, uint32_t len)
|
|||
|
|
{
|
|||
|
|
uint8_t *pUARTbuf, *pOff;
|
|||
|
|
|
|||
|
|
/*
|
|||
|
|
ɨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>֧<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD>
|
|||
|
|
(1) ScanCodeData Responce: <EFBFBD><EFBFBD>ʽΪ 5A 02 [len] [data] Bcc A5
|
|||
|
|
(2) <EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
*/
|
|||
|
|
if(gConfgBuf.uartOutputProtocal)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>FID<49><44>֯<EFBFBD><D6AF><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD>ʽΪ 5A 01/02 [len] [data] Bcc A5
|
|||
|
|
pUARTbuf = pOff = (uint8_t *) POSOUTBUF;
|
|||
|
|
*pOff++ = UART_PROTOCOL_STX_VALUE;
|
|||
|
|
*pOff++ = UART_PROTOCOL_SEND_FID_VALUE;
|
|||
|
|
*pOff++ = (uint8_t) (len >> 8);
|
|||
|
|
*pOff++ = (uint8_t)(len);
|
|||
|
|
memcpy(pOff, buf, len);
|
|||
|
|
pOff += len;
|
|||
|
|
*pOff++ = BccCheck(pUARTbuf + 1, len + 3);
|
|||
|
|
*pOff++ = UART_PROTOCOL_ETX_VALUE;
|
|||
|
|
return TMC_UART_Transmit(&ghuart1, pUARTbuf, len + 6);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return TMC_UART_Transmit(&ghuart1, buf, len);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @function AppUartReceiveData
|
|||
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @param[in] buf <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buf
|
|||
|
|
* @param[in] len <EFBFBD><EFBFBD><EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @return TMC_OK
|
|||
|
|
*/
|
|||
|
|
uint8_t AppUartReceiveData(uint8_t *buf, uint32_t len)
|
|||
|
|
{
|
|||
|
|
uint16_t realLen = 0;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڰ<EFBFBD><DAB0><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><F2BFBDB1><EFBFBD><EFBFBD>ݵ<EFBFBD>buf
|
|||
|
|
if(gUartFlag == UART_PROTOCOL_FINISH)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD>ճ<EFBFBD><D5B3><EFBFBD>С<EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>ʵ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ʴ<EFBFBD><CAB4>ڽ<EFBFBD><DABD>յ<EFBFBD><D5B5>ij<EFBFBD><C4B3><EFBFBD>
|
|||
|
|
if(len < gUartLen)
|
|||
|
|
{
|
|||
|
|
realLen = len;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
realLen = gUartLen ;//
|
|||
|
|
}
|
|||
|
|
memcpy(buf, gUartBuff + UART_PROTOCOL_DAT_OFFS, realLen);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|||
|
|
gUartFlag = UART_PROTOCOL_STX;
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>жϽ<D0B6><CFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷŵ<DDB7>gUartBuff
|
|||
|
|
SET_UART_Receive_IT(&ghuart1, gUartBuff, 1);
|
|||
|
|
}
|
|||
|
|
else if((gUartFlag != UART_PROTOCOL_STX) && (TimerHSIClockCount(TIM3, GET_TIMER) > UART_RECEIVE_TIMEOUT))
|
|||
|
|
{
|
|||
|
|
TimerHSIClockCount(TIM3, STOP_TIMER);
|
|||
|
|
UART_ClearState(&ghuart1);
|
|||
|
|
gUartFlag = UART_PROTOCOL_STX;
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>жϽ<D0B6><CFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷŵ<DDB7>gUartBuff
|
|||
|
|
SET_UART_Receive_IT(&ghuart1, gUartBuff, 1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return realLen;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @function UARTRecieveData
|
|||
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @param[in] <EFBFBD><EFBFBD>
|
|||
|
|
* @return RECIEVE_FINISH/RECEIVE_FAIL
|
|||
|
|
*/
|
|||
|
|
uint8_t UARTRecieveData(void)
|
|||
|
|
{
|
|||
|
|
uint32_t len = 0;
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
len = AppUartReceiveData(gReceiveBuf, MAX_RECEIVE_DATA_LEN);
|
|||
|
|
|
|||
|
|
if(len > 0)
|
|||
|
|
{
|
|||
|
|
gReceiveLen = len;
|
|||
|
|
return RECIEVE_FINISH;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return RECEIVE_FAIL;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief Rx Transfer completed callbacks.
|
|||
|
|
* @param[in] huart<EFBFBD><EFBFBD>pointer to a UART_HandleTypeDef structure that contains
|
|||
|
|
* the configuration information for the specified UART module.
|
|||
|
|
* @retval None
|
|||
|
|
*/
|
|||
|
|
void TMC_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
|||
|
|
{
|
|||
|
|
uint8_t ucBcc;
|
|||
|
|
// uint16_t gUartLen;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>5A,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Timer<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/*<2A><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD>
|
|||
|
|
STX:0x5A
|
|||
|
|
FID:0x00
|
|||
|
|
LEN:0xXXXX
|
|||
|
|
CMD:XXXX...
|
|||
|
|
BCC:0xXX
|
|||
|
|
ETX:0xA5
|
|||
|
|
*/
|
|||
|
|
switch(gUartFlag)
|
|||
|
|
{
|
|||
|
|
case UART_PROTOCOL_STX:
|
|||
|
|
|
|||
|
|
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ֽ<EFBFBD>Ϊ<EFBFBD><CEAA>ʼ<EFBFBD>ֽ<EFBFBD>0x5A<35><41><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ֽ<EFBFBD>*/
|
|||
|
|
if(gUartBuff[0] == UART_PROTOCOL_STX_VALUE)
|
|||
|
|
{
|
|||
|
|
TimerHSIClockCount(TIM3, START_TIMER);
|
|||
|
|
gUartFlag = UART_PROTOCOL_FID;
|
|||
|
|
gUartLen = 0;
|
|||
|
|
SET_UART_Receive_IT(&ghuart1, &gUartBuff[1], 1);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
case UART_PROTOCOL_FID:
|
|||
|
|
|
|||
|
|
/*<2A><><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6>ֽ<EFBFBD>Ϊ<EFBFBD><CEAA>ʼ<EFBFBD>ֽ<EFBFBD>0x00<30><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>*/
|
|||
|
|
/* the received data is non-encrypted */
|
|||
|
|
if(gUartBuff[1] == UART_PROTOCOL_RECEIVE_FID_VALUE)
|
|||
|
|
{
|
|||
|
|
gUartFlag = UART_PROTOCOL_LEN;
|
|||
|
|
SET_UART_Receive_IT(&ghuart1, &gUartBuff[2], 2);
|
|||
|
|
// gDataCrypted = 0;
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/*<2A><><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6>ֽ<EFBFBD>Ϊ<EFBFBD><CEAA>ʼ<EFBFBD>ֽ<EFBFBD>0x03<30><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>*/
|
|||
|
|
/* the received data is encrypted */
|
|||
|
|
if(gUartBuff[1] == PACK_TYPEA||gUartBuff[1] == PACK_TYPEB||gUartBuff[1] == PACK_TYPEC)
|
|||
|
|
{
|
|||
|
|
gUartFlag = UART_PROTOCOL_LEN;
|
|||
|
|
SET_UART_Receive_IT(&ghuart1, &gUartBuff[2], 2);
|
|||
|
|
// gDataCrypted = 1;
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
case UART_PROTOCOL_LEN:
|
|||
|
|
/*<2A><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
|||
|
|
gUartFlag = UART_PROTOCOL_DATA;
|
|||
|
|
gUartLen = (gUartBuff[2] << 8) | gUartBuff[3];
|
|||
|
|
SET_UART_Receive_IT(&ghuart1, &gUartBuff[4], gUartLen + 2);
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
case UART_PROTOCOL_DATA:
|
|||
|
|
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3>*/
|
|||
|
|
ucBcc = BccCheck(gUartBuff + 1, gUartLen + 3);
|
|||
|
|
|
|||
|
|
if((gUartBuff[gUartLen + 5] == UART_PROTOCOL_ETX_VALUE) && (gUartBuff[gUartLen + 4] == ucBcc))
|
|||
|
|
{
|
|||
|
|
TimerHSIClockCount(TIM3, STOP_TIMER);
|
|||
|
|
/*<2A><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD>*/
|
|||
|
|
gUartFlag = UART_PROTOCOL_FINISH;
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
default:
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
gUartFlag = UART_PROTOCOL_STX;
|
|||
|
|
gUartLen = 0;
|
|||
|
|
UART_ClearState(&ghuart1);
|
|||
|
|
SET_UART_Receive_IT(&ghuart1, gUartBuff, 1);
|
|||
|
|
TimerHSIClockCount(TIM3, STOP_TIMER);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief Initializes the UART mode according to the specified parameters in
|
|||
|
|
* the UART_InitTypeDef and create the associated handle.
|
|||
|
|
* @param[in] huart: pointer to a UART_HandleTypeDef structure that contains
|
|||
|
|
* the configuration information for the specified UART module.
|
|||
|
|
* @retval TMC status
|
|||
|
|
*/
|
|||
|
|
TMC_StatusTypeDef TMC_UART_Init(UART_HandleTypeDef *huart)
|
|||
|
|
{
|
|||
|
|
/* Check the UART handle allocation */
|
|||
|
|
if(huart == NULL)
|
|||
|
|
{
|
|||
|
|
return TMC_ERROR;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(huart->gState == TMC_UART_STATE_RESET)
|
|||
|
|
{
|
|||
|
|
/* Allocate lock resource and initialize it */
|
|||
|
|
huart->Lock = HAL_UNLOCKED;
|
|||
|
|
/* Init the low level hardware */
|
|||
|
|
TMC_UART_MspInit(huart);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
huart->gState = TMC_UART_STATE_BUSY;
|
|||
|
|
/* Set the UART Communication parameters */
|
|||
|
|
UART_Init(huart->UARTx, &(huart->Init));
|
|||
|
|
/* Initialize the UART state */
|
|||
|
|
huart->ErrorCode = TMC_UART_ERROR_NONE;
|
|||
|
|
huart->gState = TMC_UART_STATE_READY;
|
|||
|
|
// huart->RxState= TMC_UART_STATE_READY;
|
|||
|
|
NVIC_ClearPendingIRQ(UART1_IRQn);
|
|||
|
|
NVIC_EnableIRQ(UART1_IRQn);
|
|||
|
|
return TMC_OK;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief DeInitializes the UART peripheral.
|
|||
|
|
* @param[in] huart: pointer to a UART_HandleTypeDef structure that contains
|
|||
|
|
* the configuration information for the specified UART module.
|
|||
|
|
* @retval TMC status
|
|||
|
|
*/
|
|||
|
|
TMC_StatusTypeDef TMC_UART_DeInit(UART_HandleTypeDef *huart)
|
|||
|
|
{
|
|||
|
|
/* Check the UART handle allocation */
|
|||
|
|
if(huart == NULL)
|
|||
|
|
{
|
|||
|
|
return TMC_ERROR;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
huart->gState = TMC_UART_STATE_BUSY;
|
|||
|
|
UART_DeInit(huart->UARTx);
|
|||
|
|
huart->ErrorCode = TMC_UART_ERROR_NONE;
|
|||
|
|
huart->gState = TMC_UART_STATE_RESET;
|
|||
|
|
// huart->RxState = TMC_UART_STATE_RESET;
|
|||
|
|
/* Process Lock */
|
|||
|
|
__HAL_UNLOCK(huart);
|
|||
|
|
return TMC_OK;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
|
|||
|
|
* @param[in] huart: UART handle.
|
|||
|
|
* @retval None
|
|||
|
|
*/
|
|||
|
|
static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
|
|||
|
|
{
|
|||
|
|
/* Enable the UART Receive buffer full interrupt and Error Interrupt: (Frame error, Parity error, Overflow error) */
|
|||
|
|
UART_ITConfig(huart->UARTx, UART_FLAG_RXF | UART_FLAG_PE | UART_FLAG_OF | UART_FLAG_FE, DISABLE);
|
|||
|
|
/* At end of Rx process, restore huart->RxState to Ready */
|
|||
|
|
// huart->RxState = TMC_UART_STATE_READY;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief DMA UART communication abort callback, when initiated by TMC services on Error
|
|||
|
|
* (To be called at end of DMA Abort procedure following error occurrence).
|
|||
|
|
* @param[in] hdma: DMA handle.
|
|||
|
|
* @retval None
|
|||
|
|
*/
|
|||
|
|
static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
|
|||
|
|
{
|
|||
|
|
UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
|
|||
|
|
huart->RxXferCount = 0U;
|
|||
|
|
huart->TxXferCount = 0U;
|
|||
|
|
TMC_UART_ErrorCallback(huart);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief Sends an amount of data in query mode.
|
|||
|
|
* @param[in] huart: pointer to a UART_HandleTypeDef structure that contains
|
|||
|
|
* the configuration information for the specified UART module.
|
|||
|
|
* @param[in]: pData: Pointer to data buffer
|
|||
|
|
* @param[in]: Size: Amount of data to be sent
|
|||
|
|
* @param[in]: Timeout: Timeout duration
|
|||
|
|
* @retval TMC status
|
|||
|
|
*/
|
|||
|
|
TMC_StatusTypeDef TMC_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint32_t Size)
|
|||
|
|
{
|
|||
|
|
if(huart->gState == TMC_UART_STATE_READY)
|
|||
|
|
{
|
|||
|
|
if((pData == NULL) || (Size == 0))
|
|||
|
|
{
|
|||
|
|
return TMC_ERROR;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* Process Locked */
|
|||
|
|
// __HAL_LOCK(huart);
|
|||
|
|
huart->ErrorCode = TMC_UART_ERROR_NONE;
|
|||
|
|
huart->gState = TMC_UART_STATE_BUSY_TX;
|
|||
|
|
// /* Init tickstart for timeout managment */
|
|||
|
|
huart->TxXferSize = Size;
|
|||
|
|
huart->TxXferCount = Size;
|
|||
|
|
|
|||
|
|
while(huart->TxXferCount > 0U)
|
|||
|
|
{
|
|||
|
|
huart->TxXferCount--;
|
|||
|
|
|
|||
|
|
while(!(huart->UARTx->STS & UART_FLAG_TXE));
|
|||
|
|
|
|||
|
|
/* Wait transmit data is ready. */
|
|||
|
|
UART_SendData(huart->UARTx, *pData);
|
|||
|
|
pData++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* At end of Tx process, restore huart->gState to Ready */
|
|||
|
|
huart->gState = TMC_UART_STATE_READY;
|
|||
|
|
/* Process Unlocked */
|
|||
|
|
// __HAL_UNLOCK(huart);
|
|||
|
|
return TMC_OK;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return TMC_BUSY;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UART_Transmit(UART_HandleTypeDef *huart,uint8_t *pData, uint16_t Size)
|
|||
|
|
{
|
|||
|
|
uint16_t len = Size;
|
|||
|
|
|
|||
|
|
while(len > 0)
|
|||
|
|
{
|
|||
|
|
len--;
|
|||
|
|
while(!(huart->UARTx->STS & UART_FLAG_TXE));
|
|||
|
|
/* Wait transmit data is ready. */
|
|||
|
|
UART_SendData(huart->UARTx, *pData);
|
|||
|
|
pData++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/*
|
|||
|
|
void JJJ_Prompt(char *buf)
|
|||
|
|
{
|
|||
|
|
uint16_t len = strlen(buf);
|
|||
|
|
UART_Transmit(&ghuart1, (uint8_t *)buf, len);
|
|||
|
|
}
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief Receives an amount of data in interrupt mode.
|
|||
|
|
* @param[in] huart: pointer to a UART_HandleTypeDef structure that contains
|
|||
|
|
* the configuration information for the specified UART module.
|
|||
|
|
* @param[in] pData: Pointer to data buffer
|
|||
|
|
* @param[in] Size: Amount of data to be received
|
|||
|
|
* @retval TMC status
|
|||
|
|
*/
|
|||
|
|
TMC_StatusTypeDef SET_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint32_t Size)
|
|||
|
|
{
|
|||
|
|
// if(huart->RxState == TMC_UART_STATE_READY)
|
|||
|
|
{
|
|||
|
|
if((pData == NULL) || (Size == 0))
|
|||
|
|
{
|
|||
|
|
return TMC_ERROR;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* Process Locked */
|
|||
|
|
// __HAL_LOCK(huart);
|
|||
|
|
huart->pRxBuffPtr = pData;
|
|||
|
|
huart->RxXferSize = Size;
|
|||
|
|
huart->RxXferCount = Size;
|
|||
|
|
huart->ErrorCode = TMC_UART_ERROR_NONE;
|
|||
|
|
huart->gState = TMC_UART_STATE_READY;
|
|||
|
|
/* Process Unlocked */
|
|||
|
|
// __HAL_UNLOCK(huart);
|
|||
|
|
/* Enable the UART Receive buffer full interrupt and Error Interrupt: (Frame error, Parity error, Overflow error) */
|
|||
|
|
UART_ITConfig(huart->UARTx, UART_FLAG_RXF | UART_FLAG_PE | UART_FLAG_OF | UART_FLAG_FE, ENABLE);
|
|||
|
|
return TMC_OK;
|
|||
|
|
}
|
|||
|
|
// else
|
|||
|
|
// {
|
|||
|
|
// return TMC_BUSY;
|
|||
|
|
// }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief This function handles UART interrupt request.
|
|||
|
|
* @param[in] huart: pointer to a UART_HandleTypeDef structure that contains
|
|||
|
|
* the configuration information for the specified UART module.
|
|||
|
|
* @retval None
|
|||
|
|
*/
|
|||
|
|
void TMC_UART_IRQHandler(UART_HandleTypeDef *huart)
|
|||
|
|
{
|
|||
|
|
uint32_t errorflags = 0x00;
|
|||
|
|
uint32_t errorIT = 0x00;
|
|||
|
|
errorflags = UART_GetFlagStatus(huart->UARTx, UART_FLAG_PE | UART_FLAG_OF | UART_FLAG_FE);
|
|||
|
|
errorIT = UART_GetITStatus(huart->UARTx, UART_FLAG_PE) | UART_GetITStatus(huart->UARTx, UART_FLAG_OF) | UART_GetITStatus(huart->UARTx, UART_FLAG_FE);
|
|||
|
|
|
|||
|
|
/* If no error occurs */
|
|||
|
|
if(errorflags == RESET)
|
|||
|
|
{
|
|||
|
|
/* UART in mode Receiver -------------------------------------------------*/
|
|||
|
|
if(UART_GetITStatus(huart->UARTx, UART_FLAG_RXF) != RESET)
|
|||
|
|
{
|
|||
|
|
UART_Receive_IT(huart);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* If some errors occur */
|
|||
|
|
if((errorIT != RESET) || ((errorflags != RESET) && (UART_GetITStatus(huart->UARTx, UART_FLAG_RXF) != RESET)))
|
|||
|
|
{
|
|||
|
|
/* UART parity error interrupt occurred ----------------------------------*/
|
|||
|
|
if(UART_GetITStatus(huart->UARTx, UART_FLAG_PE) != RESET)
|
|||
|
|
{
|
|||
|
|
huart->ErrorCode |= TMC_UART_ERROR_PE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* UART frame error interrupt occurred -----------------------------------*/
|
|||
|
|
if(UART_GetITStatus(huart->UARTx, UART_FLAG_FE) != RESET)
|
|||
|
|
{
|
|||
|
|
huart->ErrorCode |= TMC_UART_ERROR_FE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* UART Over-flow interrupt occurred --------------------------------------*/
|
|||
|
|
if(UART_GetITStatus(huart->UARTx, UART_FLAG_OF) != RESET)
|
|||
|
|
{
|
|||
|
|
huart->ErrorCode |= TMC_UART_ERROR_OFE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* Call UART Error Call back function if need be --------------------------*/
|
|||
|
|
if(huart->ErrorCode != TMC_UART_ERROR_NONE)
|
|||
|
|
{
|
|||
|
|
/* Call user error callback */
|
|||
|
|
TMC_UART_ErrorCallback(huart);
|
|||
|
|
|
|||
|
|
/* UART in mode Receiver -----------------------------------------------*/
|
|||
|
|
if(UART_GetITStatus(huart->UARTx, UART_FLAG_RXF) != RESET)
|
|||
|
|
{
|
|||
|
|
UART_Receive_IT(huart);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* If Overflow error occurs, or if any error occurs in DMA mode reception,
|
|||
|
|
consider error as blocking */
|
|||
|
|
if(((huart->ErrorCode & TMC_UART_ERROR_OFE) != RESET) || (UART_GetDMAStatus(huart->UARTx, UART_DMA_RX) != RESET))
|
|||
|
|
{
|
|||
|
|
/* Blocking error : transfer is aborted
|
|||
|
|
Set the UART state ready to be able to start again the process,
|
|||
|
|
Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
|
|||
|
|
UART_EndRxTransfer(huart);
|
|||
|
|
|
|||
|
|
/* Disable the UART DMA Rx request if enabled */
|
|||
|
|
if(UART_GetDMAStatus(huart->UARTx, UART_DMA_RX) != RESET)
|
|||
|
|
{
|
|||
|
|
UART_DMACmd(huart->UARTx, UART_DMA_RX, DISABLE);
|
|||
|
|
|
|||
|
|
/* Abort the UART DMA Rx channel */
|
|||
|
|
if(huart->hdmarx != NULL)
|
|||
|
|
{
|
|||
|
|
/* Set the UART DMA Abort callback :
|
|||
|
|
will lead to call TMC_UART_ErrorCallback() at end of DMA abort procedure */
|
|||
|
|
huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
|
|||
|
|
|
|||
|
|
if(TMC_DMA_Abort_IT(huart->hdmarx) != TMC_OK)
|
|||
|
|
{
|
|||
|
|
/* Call Directly XferAbortCallback function in case of error */
|
|||
|
|
huart->hdmarx->XferAbortCallback(huart->hdmarx);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
/* Call user error callback */
|
|||
|
|
TMC_UART_ErrorCallback(huart);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
/* Call user error callback */
|
|||
|
|
TMC_UART_ErrorCallback(huart);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
/* Non Blocking error : transfer could go on.
|
|||
|
|
Error is notified to user through user error callback */
|
|||
|
|
TMC_UART_ErrorCallback(huart);
|
|||
|
|
huart->ErrorCode = TMC_UART_ERROR_NONE;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return;
|
|||
|
|
} /* End if some error occurs */
|
|||
|
|
|
|||
|
|
/* UART in mode Transmitter ------------------------------------------------*/
|
|||
|
|
if(UART_GetITStatus(huart->UARTx, UART_FLAG_TXE) != RESET)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief UART MSP Init.
|
|||
|
|
* @param[in] huart: pointer to a UART_HandleTypeDef structure that contains
|
|||
|
|
* the configuration information for the specified UART module.
|
|||
|
|
* @retval None
|
|||
|
|
*/
|
|||
|
|
void TMC_UART_MspInit(UART_HandleTypeDef *huart)
|
|||
|
|
{
|
|||
|
|
RCC_APB3PeriphClockCmd(RCC_APB3Periph_GPIO, ENABLE);
|
|||
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);
|
|||
|
|
GPIO_ControlMode(GPIOC, 8, ALTERNATE);
|
|||
|
|
GPIO_ConfigAFR1(GPIOC, 8, Function1);
|
|||
|
|
GPIO_ControlMode(GPIOC, 9, ALTERNATE);
|
|||
|
|
GPIO_ConfigAFR1(GPIOC, 9, Function1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief Receives an amount of data in non query mode
|
|||
|
|
* @param[in] huart: pointer to a UART_HandleTypeDef structure that contains
|
|||
|
|
* the configuration information for the specified UART module.
|
|||
|
|
* @retval TMC status
|
|||
|
|
*/
|
|||
|
|
static TMC_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
|
|||
|
|
{
|
|||
|
|
// if(huart->RxState == TMC_UART_STATE_BUSY_RX)
|
|||
|
|
{
|
|||
|
|
*huart->pRxBuffPtr++ = (uint8_t)(huart->UARTx->RxDATA);
|
|||
|
|
|
|||
|
|
if(--huart->RxXferCount == 0U)
|
|||
|
|
{
|
|||
|
|
/* Disable the UART Parity Error Interrupt and RXNE interrupt*/
|
|||
|
|
UART_ITConfig(huart->UARTx, UART_FLAG_RXF | UART_FLAG_PE | UART_FLAG_OF | UART_FLAG_FE, DISABLE);
|
|||
|
|
/* Tx process is ended, restore huart->gState to Ready */
|
|||
|
|
huart->gState = TMC_UART_STATE_RECEIVEFINISH;
|
|||
|
|
TMC_UART_RxCpltCallback(huart);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return TMC_OK;
|
|||
|
|
}
|
|||
|
|
// else
|
|||
|
|
// {
|
|||
|
|
// return TMC_BUSY;
|
|||
|
|
// }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief UART error callbacks.
|
|||
|
|
* @param[in] huart<EFBFBD><EFBFBD>pointer to a UART_HandleTypeDef structure that contains
|
|||
|
|
* the configuration information for the specified UART module.
|
|||
|
|
* @retval None
|
|||
|
|
*/
|
|||
|
|
__weak void TMC_UART_ErrorCallback(UART_HandleTypeDef *huart)
|
|||
|
|
{
|
|||
|
|
/* Prevent unused argument(s) compilation warning */
|
|||
|
|
UNUSED(huart);
|
|||
|
|
/* NOTE: This function Should not be modified, when the callback is needed,
|
|||
|
|
the TMC_UART_ErrorCallback could be implemented in the user file
|
|||
|
|
*/
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UART_ClearState(UART_HandleTypeDef *huart)
|
|||
|
|
{
|
|||
|
|
// huart->RxState = TMC_UART_STATE_READY;
|
|||
|
|
huart->gState = TMC_UART_STATE_READY;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/************************ (C) COPYRIGHT TMC *****END OF FILE****/
|
|||
|
|
|
|||
|
|
|