nim_uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * nim_uart.c
  3. * 配置串口
  4. * Created on: 2025年7月18日
  5. * Author: wulianwei
  6. */
  7. #include "nim_config.h"
  8. #include "uart.h"
  9. #include "tr_queue.h"
  10. /****************************************************************************/
  11. /* */
  12. /* 宏定义 */
  13. /* */
  14. /****************************************************************************/
  15. #define UART_2_FREQ SYSCLK_2_FREQ
  16. #define UART_1_FREQ SYSCLK_2_FREQ
  17. // UART0
  18. #define PINMUX3_UART0_TXD_ENABLE (SYSCFG_PINMUX3_PINMUX3_23_20_UART0_TXD << \
  19. SYSCFG_PINMUX3_PINMUX3_23_20_SHIFT)
  20. #define PINMUX3_UART0_RXD_ENABLE (SYSCFG_PINMUX3_PINMUX3_19_16_UART0_RXD << \
  21. SYSCFG_PINMUX3_PINMUX3_19_16_SHIFT)
  22. // UART1
  23. #define PINMUX4_UART1_TXD_ENABLE (SYSCFG_PINMUX4_PINMUX4_31_28_UART1_TXD << \
  24. SYSCFG_PINMUX4_PINMUX4_31_28_SHIFT)
  25. #define PINMUX4_UART1_RXD_ENABLE (SYSCFG_PINMUX4_PINMUX4_27_24_UART1_RXD << \
  26. SYSCFG_PINMUX4_PINMUX4_27_24_SHIFT)
  27. // UART2
  28. #define PINMUX4_UART2_TXD_ENABLE (SYSCFG_PINMUX4_PINMUX4_23_20_UART2_TXD << \
  29. SYSCFG_PINMUX4_PINMUX4_23_20_SHIFT)
  30. #define PINMUX4_UART2_RXD_ENABLE (SYSCFG_PINMUX4_PINMUX4_19_16_UART2_RXD << \
  31. SYSCFG_PINMUX4_PINMUX4_19_16_SHIFT)
  32. extern void UARTPinMuxSetup(unsigned int instanceNum,
  33. unsigned int modemCtrlChoice);
  34. static void UART1InterruptInit(void);
  35. static void UART1Isr();
  36. static void UART2InterruptInit(void);
  37. static void UART2Isr();
  38. /**
  39. * @Title 串口1配置
  40. */
  41. void UART1Init()
  42. {
  43. PSCModuleControl(SOC_PSC_1_REGS, HW_PSC_UART1, PSC_POWERDOMAIN_ALWAYS_ON,
  44. PSC_MDCTL_NEXT_ENABLE); // 使能 UART1 模块
  45. UARTPinMuxSetup(1, FALSE);
  46. // 配置 UART1 参数 波特率 115200 数据位 8 停止位 1 无校验位
  47. UARTConfigSetExpClk(SOC_UART_1_REGS, UART_1_FREQ, 115200,
  48. UART_WORDL_8BITS,
  49. UART_OVER_SAMP_RATE_16);
  50. // 使能 UART1
  51. UARTEnable(SOC_UART_1_REGS);
  52. // 使能接收 / 发送 FIFO
  53. UARTFIFOEnable(SOC_UART_1_REGS);
  54. // 设置 FIFO 级别
  55. UARTFIFOLevelSet(SOC_UART_1_REGS, UART_RX_TRIG_LEVEL_1);
  56. UART1InterruptInit();
  57. }
  58. /**
  59. * @Title 串口2配置
  60. */
  61. void UART2Init()
  62. {
  63. PSCModuleControl(SOC_PSC_1_REGS, HW_PSC_UART2, PSC_POWERDOMAIN_ALWAYS_ON,
  64. PSC_MDCTL_NEXT_ENABLE); // 使能 UART2 模块
  65. UARTPinMuxSetup(2, FALSE);
  66. // 配置 UART2 参数 波特率 115200 数据位 8 停止位 1 无校验位
  67. UARTConfigSetExpClk(SOC_UART_2_REGS, UART_2_FREQ, BAUD_115200,
  68. UART_WORDL_8BITS,
  69. UART_OVER_SAMP_RATE_16);
  70. // 使能 UART2
  71. UARTEnable(SOC_UART_2_REGS);
  72. // 使能接收 / 发送 FIFO
  73. UARTFIFOEnable(SOC_UART_2_REGS);
  74. // 设置 FIFO 级别
  75. UARTFIFOLevelSet(SOC_UART_2_REGS, UART_RX_TRIG_LEVEL_1);
  76. UART2InterruptInit();
  77. }
  78. /**
  79. * 配置UART gpio管脚
  80. */
  81. //static void UARTPinMuxSetup(unsigned int instanceNum, unsigned int modemCtrlChoice)
  82. //{
  83. // unsigned int svPinMuxRtsCts = 0;
  84. // unsigned int svPinMuxTxdRxd = 0;
  85. //
  86. // if(0 == instanceNum)
  87. // {
  88. // if(TRUE == modemCtrlChoice)
  89. // {
  90. // svPinMuxRtsCts = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(3)) & \
  91. // ~(SYSCFG_PINMUX3_PINMUX3_27_24 | \
  92. // SYSCFG_PINMUX3_PINMUX3_31_28));
  93. //
  94. // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(3)) = \
  95. // (PINMUX3_UART0_CTS_ENABLE | \
  96. // PINMUX3_UART0_RTS_ENABLE | \
  97. // svPinMuxRtsCts);
  98. // }
  99. //
  100. // svPinMuxTxdRxd = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(3)) & \
  101. // ~(SYSCFG_PINMUX3_PINMUX3_23_20 | \
  102. // SYSCFG_PINMUX3_PINMUX3_19_16));
  103. //
  104. // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(3)) = \
  105. // (PINMUX3_UART0_TXD_ENABLE | \
  106. // PINMUX3_UART0_RXD_ENABLE | \
  107. // svPinMuxTxdRxd);
  108. // }
  109. //
  110. // else if(1 == instanceNum)
  111. // {
  112. // if(TRUE == modemCtrlChoice)
  113. // {
  114. // svPinMuxRtsCts = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(0)) & \
  115. // ~(SYSCFG_PINMUX0_PINMUX0_23_20 | \
  116. // SYSCFG_PINMUX0_PINMUX0_19_16));
  117. //
  118. // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(0)) = \
  119. // (PINMUX0_UART1_CTS_ENABLE | \
  120. // PINMUX0_UART1_RTS_ENABLE | \
  121. // svPinMuxRtsCts);
  122. // }
  123. //
  124. // svPinMuxTxdRxd = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(4)) & \
  125. // ~(SYSCFG_PINMUX4_PINMUX4_31_28 | \
  126. // SYSCFG_PINMUX4_PINMUX4_27_24));
  127. //
  128. // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(4)) = \
  129. // (PINMUX4_UART1_TXD_ENABLE | \
  130. // PINMUX4_UART1_RXD_ENABLE | \
  131. // svPinMuxTxdRxd);
  132. // }
  133. //
  134. // else if(2 == instanceNum)
  135. // {
  136. //
  137. // if(TRUE == modemCtrlChoice)
  138. // {
  139. // svPinMuxRtsCts = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(0)) & \
  140. // ~(SYSCFG_PINMUX0_PINMUX0_31_28 | \
  141. // SYSCFG_PINMUX0_PINMUX0_27_24));
  142. //
  143. // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(0)) = \
  144. // (PINMUX0_UART2_CTS_ENABLE | \
  145. // PINMUX0_UART2_RTS_ENABLE | \
  146. // svPinMuxRtsCts);
  147. // }
  148. //
  149. // svPinMuxTxdRxd = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(4)) & \
  150. // ~(SYSCFG_PINMUX4_PINMUX4_23_20 | \
  151. // SYSCFG_PINMUX4_PINMUX4_19_16));
  152. //
  153. // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(4)) = \
  154. // (PINMUX4_UART2_TXD_ENABLE | \
  155. // PINMUX4_UART2_RXD_ENABLE | \
  156. // svPinMuxTxdRxd);
  157. //
  158. // }
  159. //
  160. // else
  161. // {
  162. //
  163. // }
  164. //}
  165. /****************************************************************************/
  166. /* */
  167. /* UART1 中断初始化 */
  168. /* */
  169. /****************************************************************************/
  170. static void UART1InterruptInit(void)
  171. {
  172. IntRegister(C674X_MASK_INT5, UART1Isr);
  173. IntEventMap(C674X_MASK_INT5, SYS_INT_UART1_INT);
  174. IntEnable(C674X_MASK_INT5);
  175. // 使能中断
  176. unsigned int intFlags = 0;
  177. intFlags |= (UART_INT_LINE_STAT |
  178. UART_INT_TX_EMPTY |
  179. UART_INT_RXDATA_CTI);
  180. UARTIntEnable(SOC_UART_1_REGS, intFlags);
  181. }
  182. /****************************************************************************/
  183. /* */
  184. /* UART2 中断初始化 */
  185. /* */
  186. /****************************************************************************/
  187. static void UART2InterruptInit(void)
  188. {
  189. IntRegister(C674X_MASK_INT4, UART2Isr);
  190. IntEventMap(C674X_MASK_INT4, SYS_INT_UART2_INT);
  191. IntEnable(C674X_MASK_INT4);
  192. // 使能中断
  193. unsigned int intFlags = 0;
  194. intFlags |= (UART_INT_LINE_STAT |
  195. UART_INT_TX_EMPTY |
  196. UART_INT_RXDATA_CTI);
  197. UARTIntEnable(SOC_UART_2_REGS, intFlags);
  198. }
  199. /****************************************************************************/
  200. /* */
  201. /* UART1 中断服务函数 */
  202. /* */
  203. /****************************************************************************/
  204. static void UART1Isr()
  205. {
  206. //static unsigned int length = sizeof(txArray);
  207. //static unsigned int count = 0;
  208. unsigned char rxData = 0;
  209. unsigned int int_id = 0;
  210. // 确定中断源
  211. int_id = UARTIntStatus(SOC_UART_1_REGS);
  212. // 清除 UART2 系统中断
  213. IntEventClear(SYS_INT_UART1_INT);
  214. // 发送中断
  215. /*if(UART_INTID_TX_EMPTY == int_id)
  216. {
  217. if(0 < length)
  218. {
  219. // 写一个字节到 THR
  220. UARTCharPutNonBlocking(SOC_UART_1_REGS, txArray[count]);
  221. length--;
  222. count++;
  223. }
  224. if(0 == length)
  225. {
  226. // 禁用发送中断
  227. UARTIntDisable(SOC_UART_1_REGS, UART_INT_TX_EMPTY);
  228. }
  229. }
  230. */
  231. // 接收中断
  232. if (UART_INTID_RX_DATA == int_id)
  233. {
  234. rxData = UARTCharGetNonBlocking(SOC_UART_1_REGS);
  235. if (UART_CONSOLE_BASE == SOC_UART_1_REGS)
  236. {
  237. //ConsoleRecvFill(rxData);
  238. enqueueFromUart(rxData); // 添加到队列
  239. }
  240. else
  241. {
  242. UARTCharPutNonBlocking(SOC_UART_1_REGS, rxData);
  243. }
  244. }
  245. // 接收错误
  246. if (UART_INTID_RX_LINE_STAT == int_id)
  247. {
  248. while (UARTRxErrorGet(SOC_UART_1_REGS))
  249. {
  250. // 从 RBR 读一个字节
  251. UARTCharGetNonBlocking(SOC_UART_1_REGS);
  252. }
  253. }
  254. return;
  255. }
  256. /****************************************************************************/
  257. /* */
  258. /* UART2 中断服务函数 */
  259. /* */
  260. /****************************************************************************/
  261. static char txArray[] = "UART2......\n\r";
  262. static void UART2Isr()
  263. {
  264. static unsigned int length = sizeof(txArray);
  265. static unsigned int count = 0;
  266. unsigned char rxData = 0;
  267. unsigned int int_id = 0;
  268. // 确定中断源
  269. int_id = UARTIntStatus(SOC_UART_2_REGS);
  270. // 清除 UART2 系统中断
  271. IntEventClear(SYS_INT_UART2_INT);
  272. // 发送中断
  273. if (UART_INTID_TX_EMPTY == int_id)
  274. {
  275. if (0 < length)
  276. {
  277. // 写一个字节到 THR
  278. UARTCharPutNonBlocking(SOC_UART_2_REGS, txArray[count]);
  279. length--;
  280. count++;
  281. }
  282. if (0 == length)
  283. {
  284. // 禁用发送中断
  285. UARTIntDisable(SOC_UART_2_REGS, UART_INT_TX_EMPTY);
  286. }
  287. }
  288. // 接收中断
  289. if (UART_INTID_RX_DATA == int_id)
  290. {
  291. rxData = UARTCharGetNonBlocking(SOC_UART_2_REGS);
  292. if (UART_CONSOLE_BASE == SOC_UART_2_REGS)
  293. {
  294. //ConsoleRecvFill(rxData);
  295. enqueueFromUart(rxData); // 添加到队列
  296. }
  297. else
  298. {
  299. UARTCharPutNonBlocking(SOC_UART_2_REGS, rxData);
  300. }
  301. }
  302. // 接收错误
  303. if (UART_INTID_RX_LINE_STAT == int_id)
  304. {
  305. while (UARTRxErrorGet(SOC_UART_2_REGS))
  306. {
  307. // 从 RBR 读一个字节
  308. UARTCharGetNonBlocking(SOC_UART_2_REGS);
  309. }
  310. }
  311. return;
  312. }