hal_uart.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. #include "hal_uart.h"
  2. #include "hc32_ll_dma.h"
  3. #include "hc32_ll_aos.h"
  4. #include "FreeRTOS.h"
  5. #include "task.h"
  6. /************************************串口2**********************************************************/
  7. __IO en_flag_status_t m_enRxFrameEnd;
  8. __IO uint16_t m_u16RxLen;
  9. uint8_t m_au8RxBuf[APP_FRAME_LEN_MAX];
  10. /*******************************************************************************
  11. * Function implementation - global ('extern') and local ('static')
  12. ******************************************************************************/
  13. /**
  14. * @brief DMA transfer complete IRQ callback function.
  15. * @param None
  16. * @retval None
  17. */
  18. static void RX_DMA_TC_IrqCallback(void)
  19. {
  20. m_enRxFrameEnd = SET;
  21. m_u16RxLen = APP_FRAME_LEN_MAX;
  22. USART_FuncCmd(USART_UNIT, USART_RX_TIMEOUT, DISABLE);
  23. DMA_ClearTransCompleteStatus(RX_DMA_UNIT, RX_DMA_TC_FLAG);
  24. }
  25. /**
  26. * @brief DMA transfer complete IRQ callback function.
  27. * @param None
  28. * @retval None
  29. */
  30. static void TX_DMA_TC_IrqCallback(void)
  31. {
  32. USART_FuncCmd(USART_UNIT, USART_INT_TX_CPLT, ENABLE);
  33. DMA_ClearTransCompleteStatus(TX_DMA_UNIT, TX_DMA_TC_FLAG);
  34. }
  35. /**
  36. * @brief Initialize DMA.
  37. * @param None
  38. * @retval int32_t:
  39. * - LL_OK: Initialize successfully.
  40. * - LL_ERR_INVD_PARAM: Initialization parameters is invalid.
  41. */
  42. static int32_t DMA_Config(void)
  43. {
  44. int32_t i32Ret;
  45. stc_dma_init_t stcDmaInit;
  46. stc_dma_llp_init_t stcDmaLlpInit;
  47. stc_irq_signin_config_t stcIrqSignConfig;
  48. static stc_dma_llp_descriptor_t stcLlpDesc;
  49. /* DMA&AOS FCG enable */
  50. RX_DMA_FCG_ENABLE();
  51. TX_DMA_FCG_ENABLE();
  52. FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_AOS, ENABLE);
  53. /* USART_RX_DMA */
  54. (void)DMA_StructInit(&stcDmaInit);
  55. stcDmaInit.u32IntEn = DMA_INT_ENABLE;
  56. stcDmaInit.u32BlockSize = 1UL;
  57. stcDmaInit.u32TransCount = ARRAY_SZ(m_au8RxBuf);
  58. stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
  59. stcDmaInit.u32DestAddr = (uint32_t)m_au8RxBuf;
  60. stcDmaInit.u32SrcAddr = ((uint32_t)(&USART_UNIT->DR) + 2UL);
  61. stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_FIX;
  62. stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_INC;
  63. i32Ret = DMA_Init(RX_DMA_UNIT, RX_DMA_CH, &stcDmaInit);
  64. if (LL_OK == i32Ret) {
  65. (void)DMA_LlpStructInit(&stcDmaLlpInit);
  66. stcDmaLlpInit.u32State = DMA_LLP_ENABLE;
  67. stcDmaLlpInit.u32Mode = DMA_LLP_WAIT;
  68. stcDmaLlpInit.u32Addr = (uint32_t)&stcLlpDesc;
  69. (void)DMA_LlpInit(RX_DMA_UNIT, RX_DMA_CH, &stcDmaLlpInit);
  70. stcLlpDesc.SARx = stcDmaInit.u32SrcAddr;
  71. stcLlpDesc.DARx = stcDmaInit.u32DestAddr;
  72. stcLlpDesc.DTCTLx = (stcDmaInit.u32TransCount << DMA_DTCTL_CNT_POS) | (stcDmaInit.u32BlockSize << DMA_DTCTL_BLKSIZE_POS);;
  73. stcLlpDesc.LLPx = (uint32_t)&stcLlpDesc;
  74. stcLlpDesc.CHCTLx = stcDmaInit.u32SrcAddrInc | stcDmaInit.u32DestAddrInc | stcDmaInit.u32DataWidth | \
  75. stcDmaInit.u32IntEn | stcDmaLlpInit.u32State | stcDmaLlpInit.u32Mode;
  76. DMA_ReconfigLlpCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
  77. DMA_ReconfigCmd(RX_DMA_UNIT, ENABLE);
  78. AOS_SetTriggerEventSrc(RX_DMA_RECONF_TRIG_SEL, RX_DMA_RECONF_TRIG_EVT_SRC);
  79. stcIrqSignConfig.enIntSrc = RX_DMA_TC_INT_SRC;
  80. stcIrqSignConfig.enIRQn = RX_DMA_TC_IRQn;
  81. stcIrqSignConfig.pfnCallback = &RX_DMA_TC_IrqCallback;
  82. (void)INTC_IrqSignIn(&stcIrqSignConfig);
  83. NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  84. NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  85. NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  86. AOS_SetTriggerEventSrc(RX_DMA_TRIG_SEL, RX_DMA_TRIG_EVT_SRC);
  87. DMA_Cmd(RX_DMA_UNIT, ENABLE);
  88. DMA_TransCompleteIntCmd(RX_DMA_UNIT, RX_DMA_TC_INT, ENABLE);
  89. (void)DMA_ChCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
  90. }
  91. /* USART_TX_DMA */
  92. (void)DMA_StructInit(&stcDmaInit);
  93. stcDmaInit.u32IntEn = DMA_INT_ENABLE;
  94. stcDmaInit.u32BlockSize = 1UL;
  95. stcDmaInit.u32TransCount = ARRAY_SZ(m_au8RxBuf);
  96. stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
  97. stcDmaInit.u32DestAddr = (uint32_t)(&USART_UNIT->DR);
  98. stcDmaInit.u32SrcAddr = (uint32_t)m_au8RxBuf;
  99. stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_INC;
  100. stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_FIX;
  101. i32Ret = DMA_Init(TX_DMA_UNIT, TX_DMA_CH, &stcDmaInit);
  102. if (LL_OK == i32Ret) {
  103. stcIrqSignConfig.enIntSrc = TX_DMA_TC_INT_SRC;
  104. stcIrqSignConfig.enIRQn = TX_DMA_TC_IRQn;
  105. stcIrqSignConfig.pfnCallback = &TX_DMA_TC_IrqCallback;
  106. (void)INTC_IrqSignIn(&stcIrqSignConfig);
  107. NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  108. NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  109. NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  110. AOS_SetTriggerEventSrc(TX_DMA_TRIG_SEL, TX_DMA_TRIG_EVT_SRC);
  111. DMA_Cmd(TX_DMA_UNIT, ENABLE);
  112. DMA_TransCompleteIntCmd(TX_DMA_UNIT, TX_DMA_TC_INT, ENABLE);
  113. }
  114. return i32Ret;
  115. }
  116. /**
  117. * @brief Configure TMR0.
  118. * @param [in] u16TimeoutBits: Timeout bits
  119. * @retval None
  120. */
  121. static void TMR0_Config(uint16_t u16TimeoutBits)
  122. {
  123. uint16_t u16Div;
  124. uint16_t u16Delay;
  125. uint16_t u16CompareValue;
  126. stc_tmr0_init_t stcTmr0Init;
  127. TMR0_FCG_ENABLE();
  128. /* Initialize TMR0 base function. */
  129. stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_XTAL32;
  130. stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV8;
  131. stcTmr0Init.u32Func = TMR0_FUNC_CMP;
  132. if (TMR0_CLK_DIV1 == stcTmr0Init.u32ClockDiv) {
  133. u16Delay = 7U;
  134. } else if (TMR0_CLK_DIV2 == stcTmr0Init.u32ClockDiv) {
  135. u16Delay = 5U;
  136. } else if ((TMR0_CLK_DIV4 == stcTmr0Init.u32ClockDiv) || \
  137. (TMR0_CLK_DIV8 == stcTmr0Init.u32ClockDiv) || \
  138. (TMR0_CLK_DIV16 == stcTmr0Init.u32ClockDiv)) {
  139. u16Delay = 3U;
  140. } else {
  141. u16Delay = 2U;
  142. }
  143. u16Div = (uint16_t)1U << (stcTmr0Init.u32ClockDiv >> TMR0_BCONR_CKDIVA_POS);
  144. u16CompareValue = ((u16TimeoutBits + u16Div - 1U) / u16Div) - u16Delay;
  145. stcTmr0Init.u16CompareValue = u16CompareValue;
  146. (void)TMR0_Init(TMR0_UNIT, TMR0_CH, &stcTmr0Init);
  147. TMR0_HWStartCondCmd(TMR0_UNIT, TMR0_CH, ENABLE);
  148. TMR0_HWClearCondCmd(TMR0_UNIT, TMR0_CH, ENABLE);
  149. }
  150. /**
  151. * @brief USART RX timeout IRQ callback.
  152. * @param None
  153. * @retval None
  154. */
  155. static void USART_RxTimeout_IrqCallback(void)
  156. {
  157. if (m_enRxFrameEnd != SET) {
  158. m_enRxFrameEnd = SET;
  159. m_u16RxLen = APP_FRAME_LEN_MAX - (uint16_t)DMA_GetTransCount(RX_DMA_UNIT, RX_DMA_CH);
  160. /* Trigger for re-config USART RX DMA */
  161. AOS_SW_Trigger();
  162. }
  163. TMR0_Stop(TMR0_UNIT, TMR0_CH);
  164. USART_ClearStatus(USART_UNIT, USART_FLAG_RX_TIMEOUT);
  165. }
  166. /**
  167. * @brief USART TX complete IRQ callback function.
  168. * @param None
  169. * @retval None
  170. */
  171. static void USART_TxComplete_IrqCallback(void)
  172. {
  173. USART_FuncCmd(USART_UNIT, (USART_TX | USART_INT_TX_CPLT), DISABLE);
  174. TMR0_Stop(TMR0_UNIT, TMR0_CH);
  175. USART_ClearStatus(USART_UNIT, USART_FLAG_RX_TIMEOUT);
  176. USART_FuncCmd(USART_UNIT, USART_RX_TIMEOUT, ENABLE);
  177. USART_ClearStatus(USART_UNIT, USART_FLAG_TX_CPLT);
  178. }
  179. /**
  180. * @brief USART RX error IRQ callback.
  181. * @param None
  182. * @retval None
  183. */
  184. static void USART_RxError_IrqCallback(void)
  185. {
  186. (void)USART_ReadData(USART_UNIT);
  187. USART_ClearStatus(USART_UNIT, (USART_FLAG_PARITY_ERR | USART_FLAG_FRAME_ERR | USART_FLAG_OVERRUN));
  188. }
  189. /**
  190. * @brief Main function of UART DMA project
  191. * @param None
  192. * @retval int32_t return value, if needed
  193. */
  194. void bsp_uart2_init(void)
  195. {
  196. stc_usart_uart_init_t stcUartInit;
  197. stc_irq_signin_config_t stcIrqSigninConfig;
  198. /* Initialize DMA. */
  199. (void)DMA_Config();
  200. /* Initialize TMR0. */
  201. TMR0_Config(USART_TIMEOUT_BITS);
  202. /* Configure USART RX/TX pin. */
  203. GPIO_SetFunc(USART_RX_PORT, USART_RX_PIN, USART_RX_GPIO_FUNC);
  204. GPIO_SetFunc(USART_TX_PORT, USART_TX_PIN, USART_TX_GPIO_FUNC);
  205. /* Enable peripheral clock */
  206. USART_FCG_ENABLE();
  207. /* Initialize UART. */
  208. (void)USART_UART_StructInit(&stcUartInit);
  209. stcUartInit.u32ClockDiv = USART_CLK_DIV64;
  210. stcUartInit.u32CKOutput = USART_CK_OUTPUT_ENABLE;
  211. stcUartInit.u32Baudrate = USART_BAUDRATE;
  212. stcUartInit.u32OverSampleBit = USART_OVER_SAMPLE_8BIT;
  213. if (LL_OK != USART_UART_Init(USART_UNIT, &stcUartInit, NULL)) {
  214. }
  215. /* Register TX complete IRQ handler. */
  216. stcIrqSigninConfig.enIRQn = USART_TX_CPLT_IRQn;
  217. stcIrqSigninConfig.enIntSrc = USART_TX_CPLT_INT_SRC;
  218. stcIrqSigninConfig.pfnCallback = &USART_TxComplete_IrqCallback;
  219. (void)INTC_IrqSignIn(&stcIrqSigninConfig);
  220. NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
  221. NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  222. NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
  223. /* Register RX error IRQ handler. */
  224. stcIrqSigninConfig.enIRQn = USART_RX_ERR_IRQn;
  225. stcIrqSigninConfig.enIntSrc = USART_RX_ERR_INT_SRC;
  226. stcIrqSigninConfig.pfnCallback = &USART_RxError_IrqCallback;
  227. (void)INTC_IrqSignIn(&stcIrqSigninConfig);
  228. NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
  229. NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  230. NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
  231. /* Register RX timeout IRQ handler. */
  232. stcIrqSigninConfig.enIRQn = USART_RX_TIMEOUT_IRQn;
  233. stcIrqSigninConfig.enIntSrc = USART_RX_TIMEOUT_INT_SRC;
  234. stcIrqSigninConfig.pfnCallback = &USART_RxTimeout_IrqCallback;
  235. (void)INTC_IrqSignIn(&stcIrqSigninConfig);
  236. NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
  237. NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  238. NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
  239. /* Enable TX && RX && RX interrupt function */
  240. USART_FuncCmd(USART_UNIT, (USART_RX | USART_INT_RX | USART_RX_TIMEOUT | \
  241. USART_INT_RX_TIMEOUT|USART_TX), ENABLE);
  242. }
  243. void test_task2(void *pvParameters)
  244. {
  245. while(1)
  246. {
  247. if (SET == m_enRxFrameEnd)
  248. {
  249. DMA_SetSrcAddr(TX_DMA_UNIT, TX_DMA_CH, (uint32_t)m_au8RxBuf);
  250. DMA_SetTransCount(TX_DMA_UNIT, TX_DMA_CH, m_u16RxLen);
  251. (void)DMA_ChCmd(TX_DMA_UNIT, TX_DMA_CH, ENABLE);
  252. USART_FuncCmd(USART_UNIT, USART_TX, ENABLE);
  253. m_enRxFrameEnd = RESET;
  254. }
  255. vTaskDelay(1);
  256. }
  257. }
  258. ///************************************串口4**********************************************************/
  259. //__IO en_flag_status_t m_enRxFrameEnd1;
  260. //__IO uint16_t m_u16RxLen1;
  261. //uint8_t m_au8RxBuf1[APP_FRAME_LEN_MAX];
  262. ///*******************************************************************************
  263. // * Function implementation - global ('extern') and local ('static')
  264. // ******************************************************************************/
  265. ///**
  266. // * @brief DMA transfer complete IRQ callback function.
  267. // * @param None
  268. // * @retval None
  269. // */
  270. //static void RX_DMA_TC_IrqCallback(void)
  271. //{
  272. // m_enRxFrameEnd = SET;
  273. // m_u16RxLen = APP_FRAME_LEN_MAX;
  274. // USART_FuncCmd(USART_UNIT, USART_RX_TIMEOUT, DISABLE);
  275. // DMA_ClearTransCompleteStatus(RX_DMA_UNIT, RX_DMA_TC_FLAG);
  276. //}
  277. ///**
  278. // * @brief DMA transfer complete IRQ callback function.
  279. // * @param None
  280. // * @retval None
  281. // */
  282. //static void TX_DMA_TC_IrqCallback(void)
  283. //{
  284. // USART_FuncCmd(USART_UNIT, USART_INT_TX_CPLT, ENABLE);
  285. // DMA_ClearTransCompleteStatus(TX_DMA_UNIT, TX_DMA_TC_FLAG);
  286. //}
  287. ///**
  288. // * @brief Initialize DMA.
  289. // * @param None
  290. // * @retval int32_t:
  291. // * - LL_OK: Initialize successfully.
  292. // * - LL_ERR_INVD_PARAM: Initialization parameters is invalid.
  293. // */
  294. //static int32_t DMA_Config(void)
  295. //{
  296. // int32_t i32Ret;
  297. // stc_dma_init_t stcDmaInit;
  298. // stc_dma_llp_init_t stcDmaLlpInit;
  299. // stc_irq_signin_config_t stcIrqSignConfig;
  300. // static stc_dma_llp_descriptor_t stcLlpDesc;
  301. // /* DMA&AOS FCG enable */
  302. // RX_DMA_FCG_ENABLE();
  303. // TX_DMA_FCG_ENABLE();
  304. // FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_AOS, ENABLE);
  305. // /* USART_RX_DMA */
  306. // (void)DMA_StructInit(&stcDmaInit);
  307. // stcDmaInit.u32IntEn = DMA_INT_ENABLE;
  308. // stcDmaInit.u32BlockSize = 1UL;
  309. // stcDmaInit.u32TransCount = ARRAY_SZ(m_au8RxBuf);
  310. // stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
  311. // stcDmaInit.u32DestAddr = (uint32_t)m_au8RxBuf;
  312. // stcDmaInit.u32SrcAddr = ((uint32_t)(&USART_UNIT->DR) + 2UL);
  313. // stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_FIX;
  314. // stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_INC;
  315. // i32Ret = DMA_Init(RX_DMA_UNIT, RX_DMA_CH, &stcDmaInit);
  316. // if (LL_OK == i32Ret) {
  317. // (void)DMA_LlpStructInit(&stcDmaLlpInit);
  318. // stcDmaLlpInit.u32State = DMA_LLP_ENABLE;
  319. // stcDmaLlpInit.u32Mode = DMA_LLP_WAIT;
  320. // stcDmaLlpInit.u32Addr = (uint32_t)&stcLlpDesc;
  321. // (void)DMA_LlpInit(RX_DMA_UNIT, RX_DMA_CH, &stcDmaLlpInit);
  322. // stcLlpDesc.SARx = stcDmaInit.u32SrcAddr;
  323. // stcLlpDesc.DARx = stcDmaInit.u32DestAddr;
  324. // stcLlpDesc.DTCTLx = (stcDmaInit.u32TransCount << DMA_DTCTL_CNT_POS) | (stcDmaInit.u32BlockSize << DMA_DTCTL_BLKSIZE_POS);;
  325. // stcLlpDesc.LLPx = (uint32_t)&stcLlpDesc;
  326. // stcLlpDesc.CHCTLx = stcDmaInit.u32SrcAddrInc | stcDmaInit.u32DestAddrInc | stcDmaInit.u32DataWidth | \
  327. // stcDmaInit.u32IntEn | stcDmaLlpInit.u32State | stcDmaLlpInit.u32Mode;
  328. // DMA_ReconfigLlpCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
  329. // DMA_ReconfigCmd(RX_DMA_UNIT, ENABLE);
  330. // AOS_SetTriggerEventSrc(RX_DMA_RECONF_TRIG_SEL, RX_DMA_RECONF_TRIG_EVT_SRC);
  331. // stcIrqSignConfig.enIntSrc = RX_DMA_TC_INT_SRC;
  332. // stcIrqSignConfig.enIRQn = RX_DMA_TC_IRQn;
  333. // stcIrqSignConfig.pfnCallback = &RX_DMA_TC_IrqCallback;
  334. // (void)INTC_IrqSignIn(&stcIrqSignConfig);
  335. // NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  336. // NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  337. // NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  338. // AOS_SetTriggerEventSrc(RX_DMA_TRIG_SEL, RX_DMA_TRIG_EVT_SRC);
  339. // DMA_Cmd(RX_DMA_UNIT, ENABLE);
  340. // DMA_TransCompleteIntCmd(RX_DMA_UNIT, RX_DMA_TC_INT, ENABLE);
  341. // (void)DMA_ChCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
  342. // }
  343. // /* USART_TX_DMA */
  344. // (void)DMA_StructInit(&stcDmaInit);
  345. // stcDmaInit.u32IntEn = DMA_INT_ENABLE;
  346. // stcDmaInit.u32BlockSize = 1UL;
  347. // stcDmaInit.u32TransCount = ARRAY_SZ(m_au8RxBuf);
  348. // stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
  349. // stcDmaInit.u32DestAddr = (uint32_t)(&USART_UNIT->DR);
  350. // stcDmaInit.u32SrcAddr = (uint32_t)m_au8RxBuf;
  351. // stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_INC;
  352. // stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_FIX;
  353. // i32Ret = DMA_Init(TX_DMA_UNIT, TX_DMA_CH, &stcDmaInit);
  354. // if (LL_OK == i32Ret) {
  355. // stcIrqSignConfig.enIntSrc = TX_DMA_TC_INT_SRC;
  356. // stcIrqSignConfig.enIRQn = TX_DMA_TC_IRQn;
  357. // stcIrqSignConfig.pfnCallback = &TX_DMA_TC_IrqCallback;
  358. // (void)INTC_IrqSignIn(&stcIrqSignConfig);
  359. // NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  360. // NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  361. // NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  362. // AOS_SetTriggerEventSrc(TX_DMA_TRIG_SEL, TX_DMA_TRIG_EVT_SRC);
  363. // DMA_Cmd(TX_DMA_UNIT, ENABLE);
  364. // DMA_TransCompleteIntCmd(TX_DMA_UNIT, TX_DMA_TC_INT, ENABLE);
  365. // }
  366. // return i32Ret;
  367. //}
  368. ///**
  369. // * @brief Configure TMR0.
  370. // * @param [in] u16TimeoutBits: Timeout bits
  371. // * @retval None
  372. // */
  373. //static void TMR0_Config(uint16_t u16TimeoutBits)
  374. //{
  375. // uint16_t u16Div;
  376. // uint16_t u16Delay;
  377. // uint16_t u16CompareValue;
  378. // stc_tmr0_init_t stcTmr0Init;
  379. // TMR0_FCG_ENABLE();
  380. // /* Initialize TMR0 base function. */
  381. // stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_XTAL32;
  382. // stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV8;
  383. // stcTmr0Init.u32Func = TMR0_FUNC_CMP;
  384. // if (TMR0_CLK_DIV1 == stcTmr0Init.u32ClockDiv) {
  385. // u16Delay = 7U;
  386. // } else if (TMR0_CLK_DIV2 == stcTmr0Init.u32ClockDiv) {
  387. // u16Delay = 5U;
  388. // } else if ((TMR0_CLK_DIV4 == stcTmr0Init.u32ClockDiv) || \
  389. // (TMR0_CLK_DIV8 == stcTmr0Init.u32ClockDiv) || \
  390. // (TMR0_CLK_DIV16 == stcTmr0Init.u32ClockDiv)) {
  391. // u16Delay = 3U;
  392. // } else {
  393. // u16Delay = 2U;
  394. // }
  395. // u16Div = (uint16_t)1U << (stcTmr0Init.u32ClockDiv >> TMR0_BCONR_CKDIVA_POS);
  396. // u16CompareValue = ((u16TimeoutBits + u16Div - 1U) / u16Div) - u16Delay;
  397. // stcTmr0Init.u16CompareValue = u16CompareValue;
  398. // (void)TMR0_Init(TMR0_UNIT, TMR0_CH, &stcTmr0Init);
  399. // TMR0_HWStartCondCmd(TMR0_UNIT, TMR0_CH, ENABLE);
  400. // TMR0_HWClearCondCmd(TMR0_UNIT, TMR0_CH, ENABLE);
  401. //}
  402. ///**
  403. // * @brief USART RX timeout IRQ callback.
  404. // * @param None
  405. // * @retval None
  406. // */
  407. //static void USART_RxTimeout_IrqCallback(void)
  408. //{
  409. // if (m_enRxFrameEnd != SET) {
  410. // m_enRxFrameEnd = SET;
  411. // m_u16RxLen = APP_FRAME_LEN_MAX - (uint16_t)DMA_GetTransCount(RX_DMA_UNIT, RX_DMA_CH);
  412. // /* Trigger for re-config USART RX DMA */
  413. // AOS_SW_Trigger();
  414. // }
  415. // TMR0_Stop(TMR0_UNIT, TMR0_CH);
  416. // USART_ClearStatus(USART_UNIT, USART_FLAG_RX_TIMEOUT);
  417. //}
  418. ///**
  419. // * @brief USART TX complete IRQ callback function.
  420. // * @param None
  421. // * @retval None
  422. // */
  423. //static void USART_TxComplete_IrqCallback(void)
  424. //{
  425. // USART_FuncCmd(USART_UNIT, (USART_TX | USART_INT_TX_CPLT), DISABLE);
  426. // TMR0_Stop(TMR0_UNIT, TMR0_CH);
  427. // USART_ClearStatus(USART_UNIT, USART_FLAG_RX_TIMEOUT);
  428. // USART_FuncCmd(USART_UNIT, USART_RX_TIMEOUT, ENABLE);
  429. // USART_ClearStatus(USART_UNIT, USART_FLAG_TX_CPLT);
  430. //}
  431. ///**
  432. // * @brief USART RX error IRQ callback.
  433. // * @param None
  434. // * @retval None
  435. // */
  436. //static void USART_RxError_IrqCallback(void)
  437. //{
  438. // (void)USART_ReadData(USART_UNIT);
  439. // USART_ClearStatus(USART_UNIT, (USART_FLAG_PARITY_ERR | USART_FLAG_FRAME_ERR | USART_FLAG_OVERRUN));
  440. //}
  441. ///**
  442. // * @brief Main function of UART DMA project
  443. // * @param None
  444. // * @retval int32_t return value, if needed
  445. // */
  446. //void uart2_init(void)
  447. //{
  448. // stc_usart_uart_init_t stcUartInit;
  449. // stc_irq_signin_config_t stcIrqSigninConfig;
  450. // /* Initialize DMA. */
  451. // (void)DMA_Config();
  452. // /* Initialize TMR0. */
  453. // TMR0_Config(USART_TIMEOUT_BITS);
  454. // /* Configure USART RX/TX pin. */
  455. // GPIO_SetFunc(USART_RX_PORT, USART_RX_PIN, USART_RX_GPIO_FUNC);
  456. // GPIO_SetFunc(USART_TX_PORT, USART_TX_PIN, USART_TX_GPIO_FUNC);
  457. // /* Enable peripheral clock */
  458. // USART_FCG_ENABLE();
  459. // /* Initialize UART. */
  460. // (void)USART_UART_StructInit(&stcUartInit);
  461. // stcUartInit.u32ClockDiv = USART_CLK_DIV64;
  462. // stcUartInit.u32CKOutput = USART_CK_OUTPUT_ENABLE;
  463. // stcUartInit.u32Baudrate = USART_BAUDRATE;
  464. // stcUartInit.u32OverSampleBit = USART_OVER_SAMPLE_8BIT;
  465. // if (LL_OK != USART_UART_Init(USART_UNIT, &stcUartInit, NULL)) {
  466. // }
  467. // /* Register TX complete IRQ handler. */
  468. // stcIrqSigninConfig.enIRQn = USART_TX_CPLT_IRQn;
  469. // stcIrqSigninConfig.enIntSrc = USART_TX_CPLT_INT_SRC;
  470. // stcIrqSigninConfig.pfnCallback = &USART_TxComplete_IrqCallback;
  471. // (void)INTC_IrqSignIn(&stcIrqSigninConfig);
  472. // NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
  473. // NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  474. // NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
  475. // /* Register RX error IRQ handler. */
  476. // stcIrqSigninConfig.enIRQn = USART_RX_ERR_IRQn;
  477. // stcIrqSigninConfig.enIntSrc = USART_RX_ERR_INT_SRC;
  478. // stcIrqSigninConfig.pfnCallback = &USART_RxError_IrqCallback;
  479. // (void)INTC_IrqSignIn(&stcIrqSigninConfig);
  480. // NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
  481. // NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  482. // NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
  483. // /* Register RX timeout IRQ handler. */
  484. // stcIrqSigninConfig.enIRQn = USART_RX_TIMEOUT_IRQn;
  485. // stcIrqSigninConfig.enIntSrc = USART_RX_TIMEOUT_INT_SRC;
  486. // stcIrqSigninConfig.pfnCallback = &USART_RxTimeout_IrqCallback;
  487. // (void)INTC_IrqSignIn(&stcIrqSigninConfig);
  488. // NVIC_ClearPendingIRQ(stcIrqSigninConfig.enIRQn);
  489. // NVIC_SetPriority(stcIrqSigninConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  490. // NVIC_EnableIRQ(stcIrqSigninConfig.enIRQn);
  491. // /* Enable TX && RX && RX interrupt function */
  492. // USART_FuncCmd(USART_UNIT, (USART_RX | USART_INT_RX | USART_RX_TIMEOUT | \
  493. // USART_INT_RX_TIMEOUT), ENABLE);
  494. //}
  495. //void test_task3(void *pvParameters)
  496. //{
  497. // while(1)
  498. // {
  499. // if (SET == m_enRxFrameEnd)
  500. // {
  501. // DMA_SetSrcAddr(TX_DMA_UNIT, TX_DMA_CH, (uint32_t)m_au8RxBuf);
  502. // DMA_SetTransCount(TX_DMA_UNIT, TX_DMA_CH, m_u16RxLen);
  503. // (void)DMA_ChCmd(TX_DMA_UNIT, TX_DMA_CH, ENABLE);
  504. // USART_FuncCmd(USART_UNIT, USART_TX, ENABLE);
  505. // m_enRxFrameEnd = RESET;
  506. // }
  507. // vTaskDelay(1);
  508. // }
  509. //}
  510. int32_t rs485_send_data(const void *pvBuf,uint32_t u32Len)
  511. {
  512. return USART_UART_Trans(CM_USART4,pvBuf,u32Len,0xff);
  513. }