| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- /*
- * nim_uart.c
- * 配置串口
- * Created on: 2025年7月18日
- * Author: wulianwei
- */
- #include "nim_config.h"
- #include "uart.h"
- #include "tr_queue.h"
- /****************************************************************************/
- /* */
- /* 宏定义 */
- /* */
- /****************************************************************************/
- #define UART_2_FREQ SYSCLK_2_FREQ
- #define UART_1_FREQ SYSCLK_2_FREQ
- // UART0
- #define PINMUX3_UART0_TXD_ENABLE (SYSCFG_PINMUX3_PINMUX3_23_20_UART0_TXD << \
- SYSCFG_PINMUX3_PINMUX3_23_20_SHIFT)
- #define PINMUX3_UART0_RXD_ENABLE (SYSCFG_PINMUX3_PINMUX3_19_16_UART0_RXD << \
- SYSCFG_PINMUX3_PINMUX3_19_16_SHIFT)
- // UART1
- #define PINMUX4_UART1_TXD_ENABLE (SYSCFG_PINMUX4_PINMUX4_31_28_UART1_TXD << \
- SYSCFG_PINMUX4_PINMUX4_31_28_SHIFT)
- #define PINMUX4_UART1_RXD_ENABLE (SYSCFG_PINMUX4_PINMUX4_27_24_UART1_RXD << \
- SYSCFG_PINMUX4_PINMUX4_27_24_SHIFT)
- // UART2
- #define PINMUX4_UART2_TXD_ENABLE (SYSCFG_PINMUX4_PINMUX4_23_20_UART2_TXD << \
- SYSCFG_PINMUX4_PINMUX4_23_20_SHIFT)
- #define PINMUX4_UART2_RXD_ENABLE (SYSCFG_PINMUX4_PINMUX4_19_16_UART2_RXD << \
- SYSCFG_PINMUX4_PINMUX4_19_16_SHIFT)
- extern void UARTPinMuxSetup(unsigned int instanceNum,
- unsigned int modemCtrlChoice);
- static void UART1InterruptInit(void);
- static void UART1Isr();
- static void UART2InterruptInit(void);
- static void UART2Isr();
- /**
- * @Title 串口1配置
- */
- void UART1Init()
- {
- PSCModuleControl(SOC_PSC_1_REGS, HW_PSC_UART1, PSC_POWERDOMAIN_ALWAYS_ON,
- PSC_MDCTL_NEXT_ENABLE); // 使能 UART1 模块
- UARTPinMuxSetup(1, FALSE);
- // 配置 UART1 参数 波特率 115200 数据位 8 停止位 1 无校验位
- UARTConfigSetExpClk(SOC_UART_1_REGS, UART_1_FREQ, 115200,
- UART_WORDL_8BITS,
- UART_OVER_SAMP_RATE_16);
- // 使能 UART1
- UARTEnable(SOC_UART_1_REGS);
- // 使能接收 / 发送 FIFO
- UARTFIFOEnable(SOC_UART_1_REGS);
- // 设置 FIFO 级别
- UARTFIFOLevelSet(SOC_UART_1_REGS, UART_RX_TRIG_LEVEL_1);
- UART1InterruptInit();
- }
- /**
- * @Title 串口2配置
- */
- void UART2Init()
- {
- PSCModuleControl(SOC_PSC_1_REGS, HW_PSC_UART2, PSC_POWERDOMAIN_ALWAYS_ON,
- PSC_MDCTL_NEXT_ENABLE); // 使能 UART2 模块
- UARTPinMuxSetup(2, FALSE);
- // 配置 UART2 参数 波特率 115200 数据位 8 停止位 1 无校验位
- UARTConfigSetExpClk(SOC_UART_2_REGS, UART_2_FREQ, BAUD_115200,
- UART_WORDL_8BITS,
- UART_OVER_SAMP_RATE_16);
- // 使能 UART2
- UARTEnable(SOC_UART_2_REGS);
- // 使能接收 / 发送 FIFO
- UARTFIFOEnable(SOC_UART_2_REGS);
- // 设置 FIFO 级别
- UARTFIFOLevelSet(SOC_UART_2_REGS, UART_RX_TRIG_LEVEL_1);
- UART2InterruptInit();
- }
- /**
- * 配置UART gpio管脚
- */
- //static void UARTPinMuxSetup(unsigned int instanceNum, unsigned int modemCtrlChoice)
- //{
- // unsigned int svPinMuxRtsCts = 0;
- // unsigned int svPinMuxTxdRxd = 0;
- //
- // if(0 == instanceNum)
- // {
- // if(TRUE == modemCtrlChoice)
- // {
- // svPinMuxRtsCts = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(3)) & \
- // ~(SYSCFG_PINMUX3_PINMUX3_27_24 | \
- // SYSCFG_PINMUX3_PINMUX3_31_28));
- //
- // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(3)) = \
- // (PINMUX3_UART0_CTS_ENABLE | \
- // PINMUX3_UART0_RTS_ENABLE | \
- // svPinMuxRtsCts);
- // }
- //
- // svPinMuxTxdRxd = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(3)) & \
- // ~(SYSCFG_PINMUX3_PINMUX3_23_20 | \
- // SYSCFG_PINMUX3_PINMUX3_19_16));
- //
- // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(3)) = \
- // (PINMUX3_UART0_TXD_ENABLE | \
- // PINMUX3_UART0_RXD_ENABLE | \
- // svPinMuxTxdRxd);
- // }
- //
- // else if(1 == instanceNum)
- // {
- // if(TRUE == modemCtrlChoice)
- // {
- // svPinMuxRtsCts = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(0)) & \
- // ~(SYSCFG_PINMUX0_PINMUX0_23_20 | \
- // SYSCFG_PINMUX0_PINMUX0_19_16));
- //
- // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(0)) = \
- // (PINMUX0_UART1_CTS_ENABLE | \
- // PINMUX0_UART1_RTS_ENABLE | \
- // svPinMuxRtsCts);
- // }
- //
- // svPinMuxTxdRxd = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(4)) & \
- // ~(SYSCFG_PINMUX4_PINMUX4_31_28 | \
- // SYSCFG_PINMUX4_PINMUX4_27_24));
- //
- // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(4)) = \
- // (PINMUX4_UART1_TXD_ENABLE | \
- // PINMUX4_UART1_RXD_ENABLE | \
- // svPinMuxTxdRxd);
- // }
- //
- // else if(2 == instanceNum)
- // {
- //
- // if(TRUE == modemCtrlChoice)
- // {
- // svPinMuxRtsCts = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(0)) & \
- // ~(SYSCFG_PINMUX0_PINMUX0_31_28 | \
- // SYSCFG_PINMUX0_PINMUX0_27_24));
- //
- // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(0)) = \
- // (PINMUX0_UART2_CTS_ENABLE | \
- // PINMUX0_UART2_RTS_ENABLE | \
- // svPinMuxRtsCts);
- // }
- //
- // svPinMuxTxdRxd = (HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(4)) & \
- // ~(SYSCFG_PINMUX4_PINMUX4_23_20 | \
- // SYSCFG_PINMUX4_PINMUX4_19_16));
- //
- // HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_PINMUX(4)) = \
- // (PINMUX4_UART2_TXD_ENABLE | \
- // PINMUX4_UART2_RXD_ENABLE | \
- // svPinMuxTxdRxd);
- //
- // }
- //
- // else
- // {
- //
- // }
- //}
- /****************************************************************************/
- /* */
- /* UART1 中断初始化 */
- /* */
- /****************************************************************************/
- static void UART1InterruptInit(void)
- {
- IntRegister(C674X_MASK_INT5, UART1Isr);
- IntEventMap(C674X_MASK_INT5, SYS_INT_UART1_INT);
- IntEnable(C674X_MASK_INT5);
- // 使能中断
- unsigned int intFlags = 0;
- intFlags |= (UART_INT_LINE_STAT |
- //UART_INT_TX_EMPTY |
- UART_INT_RXDATA_CTI);
- UARTIntEnable(SOC_UART_1_REGS, intFlags);
- }
- /****************************************************************************/
- /* */
- /* UART2 中断初始化 */
- /* */
- /****************************************************************************/
- static void UART2InterruptInit(void)
- {
- IntRegister(C674X_MASK_INT4, UART2Isr);
- IntEventMap(C674X_MASK_INT4, SYS_INT_UART2_INT);
- IntEnable(C674X_MASK_INT4);
- // 使能中断
- unsigned int intFlags = 0;
- intFlags |= (UART_INT_LINE_STAT |
- //UART_INT_TX_EMPTY |
- UART_INT_RXDATA_CTI);
- UARTIntEnable(SOC_UART_2_REGS, intFlags);
- }
- /****************************************************************************/
- /* */
- /* UART1 中断服务函数 */
- /* */
- /****************************************************************************/
- static void UART1Isr()
- {
- //static unsigned int length = sizeof(txArray);
- //static unsigned int count = 0;
- unsigned char rxData = 0;
- unsigned int int_id = 0;
- // 确定中断源
- int_id = UARTIntStatus(SOC_UART_1_REGS);
- // 清除 UART2 系统中断
- IntEventClear(SYS_INT_UART1_INT);
- // 发送中断
- /*if(UART_INTID_TX_EMPTY == int_id)
- {
- if(0 < length)
- {
- // 写一个字节到 THR
- UARTCharPutNonBlocking(SOC_UART_1_REGS, txArray[count]);
- length--;
- count++;
- }
- if(0 == length)
- {
- // 禁用发送中断
- UARTIntDisable(SOC_UART_1_REGS, UART_INT_TX_EMPTY);
- }
- }
- */
- // 接收中断
- if (UART_INTID_RX_DATA == int_id)
- {
- rxData = UARTCharGetNonBlocking(SOC_UART_1_REGS);
- if (UART_CONSOLE_BASE == SOC_UART_1_REGS)
- {
- ConsoleRecvFill(rxData);
- //enqueueFromUart(rxData); // 添加到队列
- }
- else
- {
- UARTCharPutNonBlocking(SOC_UART_1_REGS, rxData);
- }
- }
- // 接收错误
- if (UART_INTID_RX_LINE_STAT == int_id)
- {
- while (UARTRxErrorGet(SOC_UART_1_REGS))
- {
- // 从 RBR 读一个字节
- UARTCharGetNonBlocking(SOC_UART_1_REGS);
- }
- }
- return;
- }
- /****************************************************************************/
- /* */
- /* UART2 中断服务函数 */
- /* */
- /****************************************************************************/
- static char txArray[] = "UART2......\n\r";
- static void UART2Isr()
- {
- static unsigned int length = sizeof(txArray);
- static unsigned int count = 0;
- unsigned char rxData = 0;
- unsigned int int_id = 0;
- // 确定中断源
- int_id = UARTIntStatus(SOC_UART_2_REGS);
- // 清除 UART2 系统中断
- IntEventClear(SYS_INT_UART2_INT);
- // 发送中断
- if (UART_INTID_TX_EMPTY == int_id)
- {
- if (0 < length)
- {
- // 写一个字节到 THR
- UARTCharPutNonBlocking(SOC_UART_2_REGS, txArray[count]);
- length--;
- count++;
- }
- if (0 == length)
- {
- // 禁用发送中断
- UARTIntDisable(SOC_UART_2_REGS, UART_INT_TX_EMPTY);
- }
- }
- // 接收中断
- if (UART_INTID_RX_DATA == int_id)
- {
- rxData = UARTCharGetNonBlocking(SOC_UART_2_REGS);
- if (UART_CONSOLE_BASE == SOC_UART_2_REGS)
- {
- //ConsoleRecvFill(rxData);
- enqueueFromUart(rxData); // 添加到队列
- }
- else
- {
- UARTCharPutNonBlocking(SOC_UART_2_REGS, rxData);
- }
- }
- // 接收错误
- if (UART_INTID_RX_LINE_STAT == int_id)
- {
- while (UARTRxErrorGet(SOC_UART_2_REGS))
- {
- // 从 RBR 读一个字节
- UARTCharGetNonBlocking(SOC_UART_2_REGS);
- }
- }
- return;
- }
|