123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613 |
- #include "hal_uart.h"
- #include "hc32_ll_dma.h"
- #include "hc32_ll_aos.h"
- #include "FreeRTOS.h"
- #include "task.h"
- /************************************串口2**********************************************************/
- __IO en_flag_status_t m_enRxFrameEnd;
- __IO uint16_t m_u16RxLen;
- uint8_t m_au8RxBuf[APP_FRAME_LEN_MAX];
- /*******************************************************************************
- * Function implementation - global ('extern') and local ('static')
- ******************************************************************************/
- /**
- * @brief DMA transfer complete IRQ callback function.
- * @param None
- * @retval None
- */
- static void RX_DMA_TC_IrqCallback(void)
- {
- m_enRxFrameEnd = SET;
- m_u16RxLen = APP_FRAME_LEN_MAX;
- USART_FuncCmd(USART_UNIT, USART_RX_TIMEOUT, DISABLE);
- DMA_ClearTransCompleteStatus(RX_DMA_UNIT, RX_DMA_TC_FLAG);
- }
- /**
- * @brief DMA transfer complete IRQ callback function.
- * @param None
- * @retval None
- */
- static void TX_DMA_TC_IrqCallback(void)
- {
- USART_FuncCmd(USART_UNIT, USART_INT_TX_CPLT, ENABLE);
- DMA_ClearTransCompleteStatus(TX_DMA_UNIT, TX_DMA_TC_FLAG);
- }
- /**
- * @brief Initialize DMA.
- * @param None
- * @retval int32_t:
- * - LL_OK: Initialize successfully.
- * - LL_ERR_INVD_PARAM: Initialization parameters is invalid.
- */
- static int32_t DMA_Config(void)
- {
- int32_t i32Ret;
- stc_dma_init_t stcDmaInit;
- stc_dma_llp_init_t stcDmaLlpInit;
- stc_irq_signin_config_t stcIrqSignConfig;
- static stc_dma_llp_descriptor_t stcLlpDesc;
- /* DMA&AOS FCG enable */
- RX_DMA_FCG_ENABLE();
- TX_DMA_FCG_ENABLE();
- FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_AOS, ENABLE);
- /* USART_RX_DMA */
- (void)DMA_StructInit(&stcDmaInit);
- stcDmaInit.u32IntEn = DMA_INT_ENABLE;
- stcDmaInit.u32BlockSize = 1UL;
- stcDmaInit.u32TransCount = ARRAY_SZ(m_au8RxBuf);
- stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
- stcDmaInit.u32DestAddr = (uint32_t)m_au8RxBuf;
- stcDmaInit.u32SrcAddr = ((uint32_t)(&USART_UNIT->DR) + 2UL);
- stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_FIX;
- stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_INC;
- i32Ret = DMA_Init(RX_DMA_UNIT, RX_DMA_CH, &stcDmaInit);
- if (LL_OK == i32Ret) {
- (void)DMA_LlpStructInit(&stcDmaLlpInit);
- stcDmaLlpInit.u32State = DMA_LLP_ENABLE;
- stcDmaLlpInit.u32Mode = DMA_LLP_WAIT;
- stcDmaLlpInit.u32Addr = (uint32_t)&stcLlpDesc;
- (void)DMA_LlpInit(RX_DMA_UNIT, RX_DMA_CH, &stcDmaLlpInit);
- stcLlpDesc.SARx = stcDmaInit.u32SrcAddr;
- stcLlpDesc.DARx = stcDmaInit.u32DestAddr;
- stcLlpDesc.DTCTLx = (stcDmaInit.u32TransCount << DMA_DTCTL_CNT_POS) | (stcDmaInit.u32BlockSize << DMA_DTCTL_BLKSIZE_POS);;
- stcLlpDesc.LLPx = (uint32_t)&stcLlpDesc;
- stcLlpDesc.CHCTLx = stcDmaInit.u32SrcAddrInc | stcDmaInit.u32DestAddrInc | stcDmaInit.u32DataWidth | \
- stcDmaInit.u32IntEn | stcDmaLlpInit.u32State | stcDmaLlpInit.u32Mode;
- DMA_ReconfigLlpCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
- DMA_ReconfigCmd(RX_DMA_UNIT, ENABLE);
- AOS_SetTriggerEventSrc(RX_DMA_RECONF_TRIG_SEL, RX_DMA_RECONF_TRIG_EVT_SRC);
- stcIrqSignConfig.enIntSrc = RX_DMA_TC_INT_SRC;
- stcIrqSignConfig.enIRQn = RX_DMA_TC_IRQn;
- stcIrqSignConfig.pfnCallback = &RX_DMA_TC_IrqCallback;
- (void)INTC_IrqSignIn(&stcIrqSignConfig);
- NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
- NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
- AOS_SetTriggerEventSrc(RX_DMA_TRIG_SEL, RX_DMA_TRIG_EVT_SRC);
- DMA_Cmd(RX_DMA_UNIT, ENABLE);
- DMA_TransCompleteIntCmd(RX_DMA_UNIT, RX_DMA_TC_INT, ENABLE);
- (void)DMA_ChCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
- }
- /* USART_TX_DMA */
- (void)DMA_StructInit(&stcDmaInit);
- stcDmaInit.u32IntEn = DMA_INT_ENABLE;
- stcDmaInit.u32BlockSize = 1UL;
- stcDmaInit.u32TransCount = ARRAY_SZ(m_au8RxBuf);
- stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
- stcDmaInit.u32DestAddr = (uint32_t)(&USART_UNIT->DR);
- stcDmaInit.u32SrcAddr = (uint32_t)m_au8RxBuf;
- stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_INC;
- stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_FIX;
- i32Ret = DMA_Init(TX_DMA_UNIT, TX_DMA_CH, &stcDmaInit);
- if (LL_OK == i32Ret) {
- stcIrqSignConfig.enIntSrc = TX_DMA_TC_INT_SRC;
- stcIrqSignConfig.enIRQn = TX_DMA_TC_IRQn;
- stcIrqSignConfig.pfnCallback = &TX_DMA_TC_IrqCallback;
- (void)INTC_IrqSignIn(&stcIrqSignConfig);
- NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
- NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
- AOS_SetTriggerEventSrc(TX_DMA_TRIG_SEL, TX_DMA_TRIG_EVT_SRC);
- DMA_Cmd(TX_DMA_UNIT, ENABLE);
- DMA_TransCompleteIntCmd(TX_DMA_UNIT, TX_DMA_TC_INT, ENABLE);
- }
- return i32Ret;
- }
- /**
- * @brief Configure TMR0.
- * @param [in] u16TimeoutBits: Timeout bits
- * @retval None
- */
- static void TMR0_Config(uint16_t u16TimeoutBits)
- {
- uint16_t u16Div;
- uint16_t u16Delay;
- uint16_t u16CompareValue;
- stc_tmr0_init_t stcTmr0Init;
- TMR0_FCG_ENABLE();
- /* Initialize TMR0 base function. */
- stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_XTAL32;
- stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV8;
- stcTmr0Init.u32Func = TMR0_FUNC_CMP;
- if (TMR0_CLK_DIV1 == stcTmr0Init.u32ClockDiv) {
- u16Delay = 7U;
- } else if (TMR0_CLK_DIV2 == stcTmr0Init.u32ClockDiv) {
- u16Delay = 5U;
- } else if ((TMR0_CLK_DIV4 == stcTmr0Init.u32ClockDiv) || \
- (TMR0_CLK_DIV8 == stcTmr0Init.u32ClockDiv) || \
- (TMR0_CLK_DIV16 == stcTmr0Init.u32ClockDiv)) {
- u16Delay = 3U;
- } else {
- u16Delay = 2U;
- }
- u16Div = (uint16_t)1U << (stcTmr0Init.u32ClockDiv >> TMR0_BCONR_CKDIVA_POS);
- u16CompareValue = ((u16TimeoutBits + u16Div - 1U) / u16Div) - u16Delay;
- stcTmr0Init.u16CompareValue = u16CompareValue;
- (void)TMR0_Init(TMR0_UNIT, TMR0_CH, &stcTmr0Init);
- TMR0_HWStartCondCmd(TMR0_UNIT, TMR0_CH, ENABLE);
- TMR0_HWClearCondCmd(TMR0_UNIT, TMR0_CH, ENABLE);
- }
- /**
- * @brief USART RX timeout IRQ callback.
- * @param None
- * @retval None
- */
- static void USART_RxTimeout_IrqCallback(void)
- {
- if (m_enRxFrameEnd != SET) {
- m_enRxFrameEnd = SET;
- m_u16RxLen = APP_FRAME_LEN_MAX - (uint16_t)DMA_GetTransCount(RX_DMA_UNIT, RX_DMA_CH);
- /* Trigger for re-config USART RX DMA */
- AOS_SW_Trigger();
- }
- TMR0_Stop(TMR0_UNIT, TMR0_CH);
- USART_ClearStatus(USART_UNIT, USART_FLAG_RX_TIMEOUT);
- }
- /**
- * @brief USART TX complete IRQ callback function.
- * @param None
- * @retval None
- */
- static void USART_TxComplete_IrqCallback(void)
- {
- USART_FuncCmd(USART_UNIT, (USART_TX | USART_INT_TX_CPLT), DISABLE);
- TMR0_Stop(TMR0_UNIT, TMR0_CH);
- USART_ClearStatus(USART_UNIT, USART_FLAG_RX_TIMEOUT);
- USART_FuncCmd(USART_UNIT, USART_RX_TIMEOUT, ENABLE);
- USART_ClearStatus(USART_UNIT, USART_FLAG_TX_CPLT);
- }
- /**
- * @brief USART RX error IRQ callback.
- * @param None
- * @retval None
- */
- static void USART_RxError_IrqCallback(void)
- {
- (void)USART_ReadData(USART_UNIT);
- USART_ClearStatus(USART_UNIT, (USART_FLAG_PARITY_ERR | USART_FLAG_FRAME_ERR | USART_FLAG_OVERRUN));
- }
- /**
- * @brief Main function of UART DMA project
- * @param None
- * @retval int32_t return value, if needed
- */
- void bsp_uart2_init(void)
- {
- stc_usart_uart_init_t stcUartInit;
- stc_irq_signin_config_t stcIrqSigninConfig;
- /* Initialize DMA. */
- (void)DMA_Config();
- /* Initialize TMR0. */
- TMR0_Config(USART_TIMEOUT_BITS);
- /* Configure USART RX/TX pin. */
- GPIO_SetFunc(USART_RX_PORT, USART_RX_PIN, USART_RX_GPIO_FUNC);
- GPIO_SetFunc(USART_TX_PORT, USART_TX_PIN, USART_TX_GPIO_FUNC);
- /* Enable peripheral clock */
- USART_FCG_ENABLE();
- /* Initialize UART. */
- (void)USART_UART_StructInit(&stcUartInit);
- stcUartInit.u32ClockDiv = USART_CLK_DIV64;
- stcUartInit.u32CKOutput = USART_CK_OUTPUT_ENABLE;
- stcUartInit.u32Baudrate = USART_BAUDRATE;
- stcUartInit.u32OverSampleBit = USART_OVER_SAMPLE_8BIT;
- if (LL_OK != USART_UART_Init(USART_UNIT, &stcUartInit, NULL)) {
- }
- /* Register TX complete IRQ handler. */
- stcIrqSigninConfig.enIRQn = USART_TX_CPLT_IRQn;
- stcIrqSigninConfig.enIntSrc = USART_TX_CPLT_INT_SRC;
- stcIrqSigninConfig.pfnCallback = &USART_TxComplete_IrqCallback;
- (void)INTC_IrqSignIn(&stcIrqSigninConfig);
- NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
- NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
- /* Register RX error IRQ handler. */
- stcIrqSigninConfig.enIRQn = USART_RX_ERR_IRQn;
- stcIrqSigninConfig.enIntSrc = USART_RX_ERR_INT_SRC;
- stcIrqSigninConfig.pfnCallback = &USART_RxError_IrqCallback;
- (void)INTC_IrqSignIn(&stcIrqSigninConfig);
- NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
- NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
- /* Register RX timeout IRQ handler. */
- stcIrqSigninConfig.enIRQn = USART_RX_TIMEOUT_IRQn;
- stcIrqSigninConfig.enIntSrc = USART_RX_TIMEOUT_INT_SRC;
- stcIrqSigninConfig.pfnCallback = &USART_RxTimeout_IrqCallback;
- (void)INTC_IrqSignIn(&stcIrqSigninConfig);
- NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
- NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
- /* Enable TX && RX && RX interrupt function */
- USART_FuncCmd(USART_UNIT, (USART_RX | USART_INT_RX | USART_RX_TIMEOUT | \
- USART_INT_RX_TIMEOUT|USART_TX), ENABLE);
- }
- void test_task2(void *pvParameters)
- {
- while(1)
- {
- if (SET == m_enRxFrameEnd)
- {
- DMA_SetSrcAddr(TX_DMA_UNIT, TX_DMA_CH, (uint32_t)m_au8RxBuf);
- DMA_SetTransCount(TX_DMA_UNIT, TX_DMA_CH, m_u16RxLen);
- (void)DMA_ChCmd(TX_DMA_UNIT, TX_DMA_CH, ENABLE);
- USART_FuncCmd(USART_UNIT, USART_TX, ENABLE);
- m_enRxFrameEnd = RESET;
- }
- vTaskDelay(1);
- }
- }
- ///************************************串口4**********************************************************/
- //__IO en_flag_status_t m_enRxFrameEnd1;
- //__IO uint16_t m_u16RxLen1;
- //uint8_t m_au8RxBuf1[APP_FRAME_LEN_MAX];
- ///*******************************************************************************
- // * Function implementation - global ('extern') and local ('static')
- // ******************************************************************************/
- ///**
- // * @brief DMA transfer complete IRQ callback function.
- // * @param None
- // * @retval None
- // */
- //static void RX_DMA_TC_IrqCallback(void)
- //{
- // m_enRxFrameEnd = SET;
- // m_u16RxLen = APP_FRAME_LEN_MAX;
- // USART_FuncCmd(USART_UNIT, USART_RX_TIMEOUT, DISABLE);
- // DMA_ClearTransCompleteStatus(RX_DMA_UNIT, RX_DMA_TC_FLAG);
- //}
- ///**
- // * @brief DMA transfer complete IRQ callback function.
- // * @param None
- // * @retval None
- // */
- //static void TX_DMA_TC_IrqCallback(void)
- //{
- // USART_FuncCmd(USART_UNIT, USART_INT_TX_CPLT, ENABLE);
- // DMA_ClearTransCompleteStatus(TX_DMA_UNIT, TX_DMA_TC_FLAG);
- //}
- ///**
- // * @brief Initialize DMA.
- // * @param None
- // * @retval int32_t:
- // * - LL_OK: Initialize successfully.
- // * - LL_ERR_INVD_PARAM: Initialization parameters is invalid.
- // */
- //static int32_t DMA_Config(void)
- //{
- // int32_t i32Ret;
- // stc_dma_init_t stcDmaInit;
- // stc_dma_llp_init_t stcDmaLlpInit;
- // stc_irq_signin_config_t stcIrqSignConfig;
- // static stc_dma_llp_descriptor_t stcLlpDesc;
- // /* DMA&AOS FCG enable */
- // RX_DMA_FCG_ENABLE();
- // TX_DMA_FCG_ENABLE();
- // FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_AOS, ENABLE);
- // /* USART_RX_DMA */
- // (void)DMA_StructInit(&stcDmaInit);
- // stcDmaInit.u32IntEn = DMA_INT_ENABLE;
- // stcDmaInit.u32BlockSize = 1UL;
- // stcDmaInit.u32TransCount = ARRAY_SZ(m_au8RxBuf);
- // stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
- // stcDmaInit.u32DestAddr = (uint32_t)m_au8RxBuf;
- // stcDmaInit.u32SrcAddr = ((uint32_t)(&USART_UNIT->DR) + 2UL);
- // stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_FIX;
- // stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_INC;
- // i32Ret = DMA_Init(RX_DMA_UNIT, RX_DMA_CH, &stcDmaInit);
- // if (LL_OK == i32Ret) {
- // (void)DMA_LlpStructInit(&stcDmaLlpInit);
- // stcDmaLlpInit.u32State = DMA_LLP_ENABLE;
- // stcDmaLlpInit.u32Mode = DMA_LLP_WAIT;
- // stcDmaLlpInit.u32Addr = (uint32_t)&stcLlpDesc;
- // (void)DMA_LlpInit(RX_DMA_UNIT, RX_DMA_CH, &stcDmaLlpInit);
- // stcLlpDesc.SARx = stcDmaInit.u32SrcAddr;
- // stcLlpDesc.DARx = stcDmaInit.u32DestAddr;
- // stcLlpDesc.DTCTLx = (stcDmaInit.u32TransCount << DMA_DTCTL_CNT_POS) | (stcDmaInit.u32BlockSize << DMA_DTCTL_BLKSIZE_POS);;
- // stcLlpDesc.LLPx = (uint32_t)&stcLlpDesc;
- // stcLlpDesc.CHCTLx = stcDmaInit.u32SrcAddrInc | stcDmaInit.u32DestAddrInc | stcDmaInit.u32DataWidth | \
- // stcDmaInit.u32IntEn | stcDmaLlpInit.u32State | stcDmaLlpInit.u32Mode;
- // DMA_ReconfigLlpCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
- // DMA_ReconfigCmd(RX_DMA_UNIT, ENABLE);
- // AOS_SetTriggerEventSrc(RX_DMA_RECONF_TRIG_SEL, RX_DMA_RECONF_TRIG_EVT_SRC);
- // stcIrqSignConfig.enIntSrc = RX_DMA_TC_INT_SRC;
- // stcIrqSignConfig.enIRQn = RX_DMA_TC_IRQn;
- // stcIrqSignConfig.pfnCallback = &RX_DMA_TC_IrqCallback;
- // (void)INTC_IrqSignIn(&stcIrqSignConfig);
- // NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
- // NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- // NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
- // AOS_SetTriggerEventSrc(RX_DMA_TRIG_SEL, RX_DMA_TRIG_EVT_SRC);
- // DMA_Cmd(RX_DMA_UNIT, ENABLE);
- // DMA_TransCompleteIntCmd(RX_DMA_UNIT, RX_DMA_TC_INT, ENABLE);
- // (void)DMA_ChCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
- // }
- // /* USART_TX_DMA */
- // (void)DMA_StructInit(&stcDmaInit);
- // stcDmaInit.u32IntEn = DMA_INT_ENABLE;
- // stcDmaInit.u32BlockSize = 1UL;
- // stcDmaInit.u32TransCount = ARRAY_SZ(m_au8RxBuf);
- // stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
- // stcDmaInit.u32DestAddr = (uint32_t)(&USART_UNIT->DR);
- // stcDmaInit.u32SrcAddr = (uint32_t)m_au8RxBuf;
- // stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_INC;
- // stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_FIX;
- // i32Ret = DMA_Init(TX_DMA_UNIT, TX_DMA_CH, &stcDmaInit);
- // if (LL_OK == i32Ret) {
- // stcIrqSignConfig.enIntSrc = TX_DMA_TC_INT_SRC;
- // stcIrqSignConfig.enIRQn = TX_DMA_TC_IRQn;
- // stcIrqSignConfig.pfnCallback = &TX_DMA_TC_IrqCallback;
- // (void)INTC_IrqSignIn(&stcIrqSignConfig);
- // NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
- // NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- // NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
- // AOS_SetTriggerEventSrc(TX_DMA_TRIG_SEL, TX_DMA_TRIG_EVT_SRC);
- // DMA_Cmd(TX_DMA_UNIT, ENABLE);
- // DMA_TransCompleteIntCmd(TX_DMA_UNIT, TX_DMA_TC_INT, ENABLE);
- // }
- // return i32Ret;
- //}
- ///**
- // * @brief Configure TMR0.
- // * @param [in] u16TimeoutBits: Timeout bits
- // * @retval None
- // */
- //static void TMR0_Config(uint16_t u16TimeoutBits)
- //{
- // uint16_t u16Div;
- // uint16_t u16Delay;
- // uint16_t u16CompareValue;
- // stc_tmr0_init_t stcTmr0Init;
- // TMR0_FCG_ENABLE();
- // /* Initialize TMR0 base function. */
- // stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_XTAL32;
- // stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV8;
- // stcTmr0Init.u32Func = TMR0_FUNC_CMP;
- // if (TMR0_CLK_DIV1 == stcTmr0Init.u32ClockDiv) {
- // u16Delay = 7U;
- // } else if (TMR0_CLK_DIV2 == stcTmr0Init.u32ClockDiv) {
- // u16Delay = 5U;
- // } else if ((TMR0_CLK_DIV4 == stcTmr0Init.u32ClockDiv) || \
- // (TMR0_CLK_DIV8 == stcTmr0Init.u32ClockDiv) || \
- // (TMR0_CLK_DIV16 == stcTmr0Init.u32ClockDiv)) {
- // u16Delay = 3U;
- // } else {
- // u16Delay = 2U;
- // }
- // u16Div = (uint16_t)1U << (stcTmr0Init.u32ClockDiv >> TMR0_BCONR_CKDIVA_POS);
- // u16CompareValue = ((u16TimeoutBits + u16Div - 1U) / u16Div) - u16Delay;
- // stcTmr0Init.u16CompareValue = u16CompareValue;
- // (void)TMR0_Init(TMR0_UNIT, TMR0_CH, &stcTmr0Init);
- // TMR0_HWStartCondCmd(TMR0_UNIT, TMR0_CH, ENABLE);
- // TMR0_HWClearCondCmd(TMR0_UNIT, TMR0_CH, ENABLE);
- //}
- ///**
- // * @brief USART RX timeout IRQ callback.
- // * @param None
- // * @retval None
- // */
- //static void USART_RxTimeout_IrqCallback(void)
- //{
- // if (m_enRxFrameEnd != SET) {
- // m_enRxFrameEnd = SET;
- // m_u16RxLen = APP_FRAME_LEN_MAX - (uint16_t)DMA_GetTransCount(RX_DMA_UNIT, RX_DMA_CH);
- // /* Trigger for re-config USART RX DMA */
- // AOS_SW_Trigger();
- // }
- // TMR0_Stop(TMR0_UNIT, TMR0_CH);
- // USART_ClearStatus(USART_UNIT, USART_FLAG_RX_TIMEOUT);
- //}
- ///**
- // * @brief USART TX complete IRQ callback function.
- // * @param None
- // * @retval None
- // */
- //static void USART_TxComplete_IrqCallback(void)
- //{
- // USART_FuncCmd(USART_UNIT, (USART_TX | USART_INT_TX_CPLT), DISABLE);
- // TMR0_Stop(TMR0_UNIT, TMR0_CH);
- // USART_ClearStatus(USART_UNIT, USART_FLAG_RX_TIMEOUT);
- // USART_FuncCmd(USART_UNIT, USART_RX_TIMEOUT, ENABLE);
- // USART_ClearStatus(USART_UNIT, USART_FLAG_TX_CPLT);
- //}
- ///**
- // * @brief USART RX error IRQ callback.
- // * @param None
- // * @retval None
- // */
- //static void USART_RxError_IrqCallback(void)
- //{
- // (void)USART_ReadData(USART_UNIT);
- // USART_ClearStatus(USART_UNIT, (USART_FLAG_PARITY_ERR | USART_FLAG_FRAME_ERR | USART_FLAG_OVERRUN));
- //}
- ///**
- // * @brief Main function of UART DMA project
- // * @param None
- // * @retval int32_t return value, if needed
- // */
- //void uart2_init(void)
- //{
- // stc_usart_uart_init_t stcUartInit;
- // stc_irq_signin_config_t stcIrqSigninConfig;
- // /* Initialize DMA. */
- // (void)DMA_Config();
- // /* Initialize TMR0. */
- // TMR0_Config(USART_TIMEOUT_BITS);
- // /* Configure USART RX/TX pin. */
- // GPIO_SetFunc(USART_RX_PORT, USART_RX_PIN, USART_RX_GPIO_FUNC);
- // GPIO_SetFunc(USART_TX_PORT, USART_TX_PIN, USART_TX_GPIO_FUNC);
- // /* Enable peripheral clock */
- // USART_FCG_ENABLE();
- // /* Initialize UART. */
- // (void)USART_UART_StructInit(&stcUartInit);
- // stcUartInit.u32ClockDiv = USART_CLK_DIV64;
- // stcUartInit.u32CKOutput = USART_CK_OUTPUT_ENABLE;
- // stcUartInit.u32Baudrate = USART_BAUDRATE;
- // stcUartInit.u32OverSampleBit = USART_OVER_SAMPLE_8BIT;
- // if (LL_OK != USART_UART_Init(USART_UNIT, &stcUartInit, NULL)) {
- // }
- // /* Register TX complete IRQ handler. */
- // stcIrqSigninConfig.enIRQn = USART_TX_CPLT_IRQn;
- // stcIrqSigninConfig.enIntSrc = USART_TX_CPLT_INT_SRC;
- // stcIrqSigninConfig.pfnCallback = &USART_TxComplete_IrqCallback;
- // (void)INTC_IrqSignIn(&stcIrqSigninConfig);
- // NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
- // NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- // NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
- // /* Register RX error IRQ handler. */
- // stcIrqSigninConfig.enIRQn = USART_RX_ERR_IRQn;
- // stcIrqSigninConfig.enIntSrc = USART_RX_ERR_INT_SRC;
- // stcIrqSigninConfig.pfnCallback = &USART_RxError_IrqCallback;
- // (void)INTC_IrqSignIn(&stcIrqSigninConfig);
- // NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
- // NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- // NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
- // /* Register RX timeout IRQ handler. */
- // stcIrqSigninConfig.enIRQn = USART_RX_TIMEOUT_IRQn;
- // stcIrqSigninConfig.enIntSrc = USART_RX_TIMEOUT_INT_SRC;
- // stcIrqSigninConfig.pfnCallback = &USART_RxTimeout_IrqCallback;
- // (void)INTC_IrqSignIn(&stcIrqSigninConfig);
- // NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
- // NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- // NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
- // /* Enable TX && RX && RX interrupt function */
- // USART_FuncCmd(USART_UNIT, (USART_RX | USART_INT_RX | USART_RX_TIMEOUT | \
- // USART_INT_RX_TIMEOUT), ENABLE);
- //}
- //void test_task3(void *pvParameters)
- //{
- // while(1)
- // {
- // if (SET == m_enRxFrameEnd)
- // {
- // DMA_SetSrcAddr(TX_DMA_UNIT, TX_DMA_CH, (uint32_t)m_au8RxBuf);
- // DMA_SetTransCount(TX_DMA_UNIT, TX_DMA_CH, m_u16RxLen);
- // (void)DMA_ChCmd(TX_DMA_UNIT, TX_DMA_CH, ENABLE);
- // USART_FuncCmd(USART_UNIT, USART_TX, ENABLE);
- // m_enRxFrameEnd = RESET;
- // }
- // vTaskDelay(1);
- // }
- //}
- int32_t rs485_send_data(const void *pvBuf,uint32_t u32Len)
- {
- return USART_UART_Trans(CM_USART4,pvBuf,u32Len,0xff);
- }
-
|