static void Usart3RxIrqCallback(void)
{
read = USART_RecData(USART3_CH);
if(RXflage == false)
{
RXbuff[readdata] = read;
if((RXbuff[0] & 0xFF) == 0xAC)
{
readdata++;
if(readdata >= 8)
{
readdata = 0;
RXflage = true;
}
}
}
}
static void Usart3TxIrqCallback(void)
{
if(TXflage == true)
{ USART_SendData(USART3_CH, TXbuff[writedata+1]);
writedata++;
if(writedata >= 7)
{
writedata = 0;
TXflage = false;
}
}
else if(TXflage == false)
{
//USART_FuncCmd(USART3_CH, UsartTxEmptyInt, Disable);
//USART_FuncCmd(USART3_CH, UsartTxCmpltInt, Enable);
while (Reset == USART_GetStatus(USART3_CH, UsartTxComplete))
{
}
EN_485_L;
}
}
static void Usart3ErrIrqCallback(void)
{
if (Set == USART_GetStatus(USART3_CH, UsartFrameErr))
USART_ClearStatus(USART3_CH, UsartFrameErr);
if (Set == USART_GetStatus(USART3_CH, UsartParityErr))
USART_ClearStatus(USART3_CH, UsartParityErr);
if (Set == USART_GetStatus(USART3_CH, UsartOverrunErr))
USART_ClearStatus(USART3_CH, UsartOverrunErr);
if (Set == USART_GetStatus(USART3_CH, UsartRxNoEmpty))
USART_ClearStatus(USART3_CH, UsartRxNoEmpty);
if (Set == USART_GetStatus(USART3_CH, UsartTxComplete))
USART_ClearStatus(USART3_CH, UsartTxComplete);
if (Set == USART_GetStatus(USART3_CH, UsartTxEmpty))
USART_ClearStatus(USART3_CH, UsartTxEmpty);
if (Set == USART_GetStatus(USART3_CH, UsartRxTimeOut))
USART_ClearStatus(USART3_CH, UsartRxTimeOut);
if (Set == USART_GetStatus(USART3_CH, UsartRxMpb))
USART_ClearStatus(USART3_CH, UsartRxMpb);
}
static void Usart3TimeoutIrqCallback(void)
{
TIMER0_Cmd(TMR_UNIT,Tim0_ChannelA,Disable);
//USART3_CH->CR1_f.CRTOF = 1ul;
USART_ClearStatus(USART3_CH, UsartRxTimeOut);
}
static void Usart3TxCmpltIrqCallback(void)
{
USART_FuncCmd(USART3_CH,UsartTx,Disable);
USART_FuncCmd(USART3_CH,UsartTxCmpltInt,Disable);
}
void Uart3_Init(void)
{
en_result_t enRet = Ok;
stc_irq_regi_conf_t stcIrqRegiCfg;
const stc_usart_uart_init_t stcInitCfg = {
UsartIntClkCkNoOutput,
//UsartIntClkCkOutput,
UsartClkDiv_64,
UsartDataBits8,
UsartDataLsbFirst,
UsartOneStopBit,
UsartParityNone,
UsartSampleBit8,
UsartStartBitFallEdge,
UsartRtsEnable,
};
PWC_Fcg1PeriphClockCmd(PWC_FCG1_PERIPH_USART3,Enable);
PORT_SetFunc(USART3_RX_PORT,USART3_RX_PIN,USART3_RX_FUNC,Disable);
PORT_SetFunc(USART3_TX_PORT,USART3_TX_PIN,USART3_TX_FUNC,Disable);
enRet = USART_UART_Init(USART3_CH,&stcInitCfg);
if(enRet != Ok)
{
while(1)
{
}
}
enRet = USART_SetBaudrate(USART3_CH,USART3_BAUDRATE);
if(enRet != Ok)
{
while(1)
{
}
}
stcIrqRegiCfg.enIRQn = Int001_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart3RxIrqCallback;
stcIrqRegiCfg.enIntSrc = USART3_RI_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
stcIrqRegiCfg.enIRQn = Int002_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart3ErrIrqCallback;
stcIrqRegiCfg.enIntSrc = USART3_EI_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
stcIrqRegiCfg.enIRQn = Int008_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart3TimeoutIrqCallback;
stcIrqRegiCfg.enIntSrc = USART3_RTO_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
stcIrqRegiCfg.enIRQn = Int000_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart3TxIrqCallback;
stcIrqRegiCfg.enIntSrc = USART3_TI_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
stcIrqRegiCfg.enIRQn = Int004_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart3TxCmpltIrqCallback;
stcIrqRegiCfg.enIntSrc = USART3_TCI_NUM;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
USART_FuncCmd(USART3_CH, UsartRx, Enable);
USART_FuncCmd(USART3_CH, UsartRxInt, Enable);
USART_FuncCmd(USART3_CH, UsartTx, Enable);
USART_FuncCmd(USART3_CH, UsartTimeOut, Enable);
USART_FuncCmd(USART3_CH, UsartTimeOutInt, Enable);
USART_FuncCmd(USART3_CH, UsartTxEmptyInt, Enable);
//USART_SetMode(USART3_CH, UsartUartMode);
}