Files
LFP_48V_mBMS_FW/APP_R3221/App/Hardware/usart.c

342 lines
10 KiB
C

/**
******************************************************************************
* @file usart.c
* @author AMG Energy Solution Team
* @version V1.0.0
* @date 07-Feb-2022
* @brief This file provides all the codes of the usart comm. functions.
******************************************************************************
* @attention
*
* 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, STMICROELECTRONICS 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>&copy; COPYRIGHT 2022 AMG</center></h2>
******************************************************************************
*/
/** @addtogroup mBMS2
* @{
*/
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
#include <includes.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#ifdef CONSOLE_DEBUG
/* USART channel definition */
#define DEBUG_CH (M4_USART1)
/* USART baudrate definition */
#define DEBUG_BAUDRATE (115200ul)
/* USART RX Port/Pin definition */
#define DEBUG_RX_PORT (PortA)
#define DEBUG_RX_PIN (Pin10)
#define DEBUG_RX_FUNC (Func_Usart1_Rx)
/* USART TX Port/Pin definition */
#define DEBUG_TX_PORT (PortA)
#define DEBUG_TX_PIN (Pin09)
#define DEBUG_TX_FUNC (Func_Usart1_Tx)
/* USART interrupt number */
#define DEBUG_RI_NUM (INT_USART1_RI)
#define DEBUG_EI_NUM (INT_USART1_EI)
#define DEBUG_TI_NUM (INT_USART1_TI)
#define DEBUG_TCI_NUM (INT_USART1_TCI)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void debug_port_init(void);
void DebugRxIrqCallback(void); //Serial port 1 interrupt service routine
void DebugTxIrqCallback(void); //Serial port 1 interrupt service routine
void DebugErrIrqCallback(void);
void DebugTxCmpltIrqCallback(void);
#endif /* CONSOLE_DEBUG */
/* Private functions ---------------------------------------------------------*/
/**
*******************************************************************************
** \brief Re-target fputc function.
**
** \param [in] ch
** \param [in] f
**
** \retval int32_t
**
******************************************************************************/
int32_t fputc(int32_t ch, FILE *f)
{
(void)f; /* Prevent unused argument compilation warning */
#ifdef CONSOLE_DEBUG
Debug_PutChar(ch);
#endif // #ifdef CONSOLE_DEBUG
return ch;
}
#ifdef CONSOLE_DEBUG
UART1_Info Uart1;
//Initializes IO serial port 1
//Bound: baud rate
void debug_usart_init(void)
{
en_result_t enRet = Ok;
stc_irq_regi_conf_t stcIrqRegiCfg;
const stc_usart_uart_init_t stcInitCfg = {
UsartIntClkCkNoOutput,
UsartClkDiv_1,
UsartDataBits8,
UsartDataLsbFirst,
UsartOneStopBit,
UsartParityNone,
UsartSampleBit8,
UsartStartBitFallEdge,
UsartRtsEnable,
};
debug_port_init();
/* Initialize UART */
enRet = USART_UART_Init(DEBUG_CH, &stcInitCfg);
if (enRet != Ok)
{
while (1)
{
}
}
/* Set baudrate */
enRet = USART_SetBaudrate(DEBUG_CH, DEBUG_BAUDRATE);
if (enRet != Ok)
{
while (1)
{
}
}
/* Set USART RX IRQ */
stcIrqRegiCfg.enIRQn = Int000_IRQn;
stcIrqRegiCfg.pfnCallback = &DebugRxIrqCallback;
stcIrqRegiCfg.enIntSrc = DEBUG_RI_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
/* Set USART RX error IRQ */
stcIrqRegiCfg.enIRQn = Int001_IRQn;
stcIrqRegiCfg.pfnCallback = &DebugErrIrqCallback;
stcIrqRegiCfg.enIntSrc = DEBUG_EI_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
/* Set USART TX IRQ */
stcIrqRegiCfg.enIRQn = Int002_IRQn;
stcIrqRegiCfg.pfnCallback = &DebugTxIrqCallback;
stcIrqRegiCfg.enIntSrc = DEBUG_TI_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
/* Set USART TX complete IRQ */
stcIrqRegiCfg.enIRQn = Int003_IRQn;
stcIrqRegiCfg.pfnCallback = &DebugTxCmpltIrqCallback;
stcIrqRegiCfg.enIntSrc = DEBUG_TCI_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
/*Enable RX && RX interupt function*/
USART_FuncCmd(DEBUG_CH, UsartRx, Enable);
// USART_FuncCmd(DEBUG_CH, UsartTx, Enable);
USART_FuncCmd(DEBUG_CH, UsartRxInt, Enable);
// USART_FuncCmd(DEBUG_CH, UsartTxAndTxEmptyInt, Enable);
}
void debug_port_init(void)
{
uint32_t u32Fcg1Periph = PWC_FCG1_PERIPH_USART1 | PWC_FCG1_PERIPH_USART2 | \
PWC_FCG1_PERIPH_USART3 | PWC_FCG1_PERIPH_USART4;
/* Enable peripheral clock */
PWC_Fcg1PeriphClockCmd(u32Fcg1Periph, Enable);
/* Initialize USART IO */
PORT_SetFunc(DEBUG_RX_PORT, DEBUG_RX_PIN, DEBUG_RX_FUNC, Disable);
PORT_SetFunc(DEBUG_TX_PORT, DEBUG_TX_PIN, DEBUG_TX_FUNC, Disable);
}
/**
*******************************************************************************
** \brief Debug RX irq callback function.
**
** \param [in] None
**
** \retval None
**
******************************************************************************/
void DebugRxIrqCallback(void) //Serial port 1 interrupt service routine
{
uint8_t rx_data;
if (Set == USART_GetStatus(DEBUG_CH, UsartRxNoEmpty))
{
rx_data = USART_RecData(DEBUG_CH) & 0xFF; /* Read one byte from the receive data register. */
Uart1.RxBuf[Uart1.RxInPos++] = rx_data;
Uart1.RxInPos %= UART1_RX_BUFFER_SIZE;
if (Uart1.RxInPos == Uart1.RxOutPos)
{
Uart1.RxOutPos++;
Uart1.RxOutPos %= UART1_RX_BUFFER_SIZE;
}
}
NVIC_ClearPendingIRQ(Int000_IRQn);
}
/**
*******************************************************************************
** \brief Debug RX error irq callback function.
**
** \param [in] None
**
** \retval None
**
******************************************************************************/
static void DebugErrIrqCallback(void)
{
if (Set == USART_GetStatus(DEBUG_CH, UsartFrameErr))
{
USART_ClearStatus(DEBUG_CH, UsartFrameErr);
}
if (Set == USART_GetStatus(DEBUG_CH, UsartParityErr))
{
USART_ClearStatus(DEBUG_CH, UsartParityErr);
}
if (Set == USART_GetStatus(DEBUG_CH, UsartOverrunErr))
{
USART_ClearStatus(DEBUG_CH, UsartOverrunErr);
}
NVIC_ClearPendingIRQ(Int001_IRQn);
}
void DebugTxIrqCallback(void) //Serial port 1 interrupt service routine
{
if (Set == USART_GetStatus(DEBUG_CH, UsartTxEmpty))
{
/* Write one byte to the transmit data register */
USART_SendData(DEBUG_CH, Uart1.TxBuf[Uart1.TxOutPos++]);
Uart1.TxOutPos %= UART1_TX_BUFFER_SIZE;
if (Uart1.TxOutPos == Uart1.TxInPos)
{
/* Disable the USART1 Transmit interrupt */
USART_FuncCmd(DEBUG_CH, UsartTxEmptyInt, Disable);
USART_FuncCmd(DEBUG_CH, UsartTxCmpltInt, Enable);
}
}
NVIC_ClearPendingIRQ(Int002_IRQn);
}
/**
*******************************************************************************
** \brief USART TX complete irq callback function.
**
** \param [in] None
**
** \retval None
**
******************************************************************************/
static void DebugTxCmpltIrqCallback(void)
{
USART_FuncCmd(DEBUG_CH, UsartTxCmpltInt, Disable);
USART_FuncCmd(DEBUG_CH, UsartTx, Disable);
NVIC_ClearPendingIRQ(Int003_IRQn);
}
/*******************************************************************************
* Function Name : void UART1_PutChar(char data)
* Description : Send 1 byte via UART1
* Parameters : ASCII code
* Return : None
*******************************************************************************/
void Debug_PutChar(const uint8_t ascii)
{
while (((Uart1.TxInPos + 1)% UART1_TX_BUFFER_SIZE) == (Uart1.TxOutPos % UART1_TX_BUFFER_SIZE))
USART_FuncCmd(DEBUG_CH, UsartTxAndTxEmptyInt, Enable);
Uart1.TxBuf[Uart1.TxInPos++] = ascii;
Uart1.TxInPos %= UART1_TX_BUFFER_SIZE;
USART_FuncCmd(DEBUG_CH, UsartTxAndTxEmptyInt, Enable);
}
/*******************************************************************************
* Function Name : uint8_t UART1_GetChar(char *data)
* Description : Send 1 byte data to UART1 transmit buffer
* Parameters : none
* Return : Transmission result (0: buffer empty, 1: buffer has data)
*******************************************************************************/
int Debug_GetChar(uint8_t *data)
{
if (Uart1.RxInPos != Uart1.RxOutPos)
{
data[0] = Uart1.RxBuf[Uart1.RxOutPos];
Uart1.RxOutPos++;
Uart1.RxOutPos %= UART1_RX_BUFFER_SIZE;
return 1;
}
else
return 0;
}
/*******************************************************************************
* Function Name : uint8_t UART1_GetChar(char *data)
* Description : Send 1 byte data to UART1 transmit buffer
* Parameters : none
* Return : Transmission result (0: buffer empty, 1: buffer has data)
*******************************************************************************/
uint8_t UART1_GC(void)
{
uint8_t result = 0;
if (Uart1.RxInPos != Uart1.RxOutPos)
{
result = Uart1.RxBuf[Uart1.RxOutPos];
Uart1.RxOutPos++;
Uart1.RxOutPos %= UART1_RX_BUFFER_SIZE;
}
return result;
}
#endif // #ifdef CONSOLE_DEBUG
/*******************(C)COPYRIGHT 2022 AMG ********************END OF FILE******/