串口初始化
/**
*******************************************************************************
** \brief Usart_init function of project
**
** \param None
**
** \retval void
**
******************************************************************************/
void Usart_init(void)
{
en_result_t enRet = Ok;
stc_irq_regi_conf_t stcIrqRegiCfg;
stc_port_init_t stc_485_RE_PortInit,stc_485_TX_PortInit;
uint32_t u32Fcg1Periph = PWC_FCG1_PERIPH_USART1 | PWC_FCG1_PERIPH_USART2 | \
PWC_FCG1_PERIPH_USART3 | PWC_FCG1_PERIPH_USART4;
const stc_usart_uart_init_t stcInitCfg = {
UsartIntClkCkNoOutput,//时钟源可选:内部时钟源(内部波特率生成器生成的时钟)/外部时钟源( CKn管脚输入的时钟)
UsartClkDiv_1,
UsartDataBits8,//数据长度可编程:8位/9位
UsartDataLsbFirst,
UsartOneStopBit,//停止位可配置:1 位/2位
UsartParityNone,//校验功能可配置:奇校验/偶校验/无校验
UsartSampleBit8,
UsartStartBitFallEdge,
UsartRtsEnable,
};
/* Initialize Timer0 */
Timer0Init();
/* Initialize DMA */
DmaInit();
DmaInit_Tx();
/* Enable peripheral clock */
PWC_Fcg1PeriphClockCmd(u32Fcg1Periph, Enable);
/* Initialize USART IO */
PORT_SetFunc(USART_RX_PORT, USART_RX_PIN, USART_RX_FUNC, Disable);
MEM_ZERO_STRUCT(stc_485_TX_PortInit);
stc_485_TX_PortInit.enPullUp=Enable;
PORT_Init(USART_TX_PORT,USART_TX_PIN,&stc_485_TX_PortInit);
PORT_SetFunc(USART_TX_PORT, USART_TX_PIN, USART_TX_FUNC, Disable);
/* configuration RS485_RE structure initialization */
MEM_ZERO_STRUCT(stc_485_RE_PortInit);
stc_485_RE_PortInit.enPinMode = Pin_Mode_Out;
PORT_Init(USART_RE_PORT, USART_RE_PIN, &stc_485_RE_PortInit);
PORT_ResetBits(USART_RE_PORT, USART_RE_PIN);
/* Initialize UART */
enRet = USART_UART_Init(USART_CH, &stcInitCfg);
if (enRet != Ok)
{
while (1)
{
}
}
/* Set baudrate */
enRet = USART_SetBaudrate(USART_CH, USART_BAUDRATE);
if (enRet != Ok)
{
while (1)
{
}
}
/* Set USART RX IRQ */
stcIrqRegiCfg.enIRQn = USART_RI_IRQn;
stcIrqRegiCfg.pfnCallback = &UsartRxIrqCallback;
stcIrqRegiCfg.enIntSrc = USART_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 = USART_EI_IRQn;
stcIrqRegiCfg.pfnCallback = &UsartErrIrqCallback;
stcIrqRegiCfg.enIntSrc = USART_EI_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
/* Set USART RX timeout error IRQ */
stcIrqRegiCfg.enIRQn = USART_RTO_IRQn;
stcIrqRegiCfg.pfnCallback = &UsartTimeoutIrqCallback;
stcIrqRegiCfg.enIntSrc = USART_RTO_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 = USART_TI_IRQn;
stcIrqRegiCfg.pfnCallback = &UsartTxIrqCallback;
stcIrqRegiCfg.enIntSrc = USART_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 = USART_TCI_IRQn;
stcIrqRegiCfg.pfnCallback = &UsartTxCmpltIrqCallback;
stcIrqRegiCfg.enIntSrc = USART_TCI_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
/*Enable RX && RX interupt && timeout interrupt function*/
USART_FuncCmd(USART_CH, UsartNoiseFilter,Enable);
USART_FuncCmd(USART_CH, UsartRx, Enable);
USART_FuncCmd(USART_CH, UsartRxInt, Enable);
USART_FuncCmd(USART_CH, UsartTimeOut, Enable);
USART_FuncCmd(USART_CH, UsartTimeOutInt, Enable);
/*Enable TX && RX && RX interrupt function*/
}
|