You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							113 lines
						
					
					
						
							2.8 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							113 lines
						
					
					
						
							2.8 KiB
						
					
					
				| #include "api-uart.h" | |
| #include "stdio.h" | |
| #include "stdarg.h" | |
| #include "stdlib.h" | |
|  | |
| // 默认串口数2 | |
| Uart_t UartList[UART_NUMBER]; | |
| 
 | |
| 
 | |
| void Uart_Init(uint8_t uartId, GpioPin_t Tx, GpioPin_t Rx, uint32_t baudRate, DataBits_t dataBits, | |
|                StopBits_t stopBits, Parity_t parity) | |
| { | |
|     uint32_t baud = 0; | |
|     Fifo_Init(&UartList[uartId].RxFifo, UartList[uartId].RxBuff, RxSize); | |
|     Gpio_Set(Tx); | |
|     Gpio_Init(Tx, GPIO_ModeOut_PP_5mA); | |
|     Gpio_Init(Rx, GPIO_ModeIN_PU); | |
| 
 | |
|     baud = 10 * GetSysClock() / 8 / baudRate; | |
|     baud = (baud + 5) / 10; | |
|     Uart(uartId)->DL = (uint16_t) baud; | |
|     Uart(uartId)->FCR = (2 << 6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN; | |
|     Uart(uartId)->LCR = dataBits | (stopBits << 2) | (parity == Parity_None ? 0 : (parity - 1) << 3); | |
|     Uart(uartId)->IER = RB_IER_TXD_EN; | |
|     Uart(uartId)->DIV = 1; | |
|     Uart_INCfg(uartId, ENABLE, RB_IER_RECV_RDY | RB_IER_LINE_STAT); | |
|     switch (uartId) | |
|     { | |
|         case 0: | |
|             PFIC_EnableIRQ(UART0_IRQn); | |
|             break; | |
|         case 1: | |
|             PFIC_EnableIRQ(UART1_IRQn); | |
|             break; | |
|         case 2: | |
|             PFIC_EnableIRQ(UART2_IRQn); | |
|             break; | |
|         case 3: | |
|             PFIC_EnableIRQ(UART3_IRQn); | |
|             break; | |
|         default: | |
|             break; | |
|     } | |
| } | |
| 
 | |
| void Uart_INCfg(uint8_t uartId, FunctionalState s, uint8_t i) | |
| { | |
|     if (s == ENABLE) | |
|     { | |
|         Uart(uartId)->IER |= i; | |
|         Uart(uartId)->MCR |= RB_MCR_INT_OE; | |
|     } else | |
|     { | |
|         Uart(uartId)->IER &= ~i; | |
|     } | |
| } | |
| 
 | |
| /** | |
|  * @brief 自定义printf函数 | |
|  *  这个函数在CMakelists.txt中添加了printf全局宏定义,使用printf函数时会调用这个函数 | |
|  */ | |
| int myPrintf(const char *fmt, ...) | |
| { | |
|     va_list arg; | |
|     va_start(arg, fmt); | |
|     int len = vsnprintf(NULL, 0, fmt, arg); | |
|     uint8_t *data = (uint8_t *) malloc(len); | |
|     vsprintf((char *) data, fmt, arg); | |
|     UART1_SendString(data, len); // 修改为你的串口发送函数 | |
|     free(data); | |
|     va_end(arg); | |
|     data = NULL; | |
|     return 0; | |
| } | |
| 
 | |
| bool Api_GetUartData(uint8_t id, uint8_t *cmd) | |
| { | |
|     if (!Fifo_IsEmpty(&UartList[id].RxFifo)) | |
|     { | |
|         *cmd = Fifo_Pop(&UartList[id].RxFifo); | |
|         return true; | |
|     } | |
|     return false; | |
| } | |
| 
 | |
| 
 | |
| __HIGH_CODE | |
| void UART1_IRQHandler(void) | |
| { | |
|     uint8_t cmd = 0; | |
|     if (UART1_GetITFlag() == UART_II_RECV_RDY) | |
|     { | |
|         if (R8_UART1_RFC) | |
|         { | |
|             if (!Fifo_IsFull(&UartList[1].RxFifo)) | |
|             { | |
|                 Fifo_Push(&UartList[1].RxFifo, R8_UART1_RBR); | |
|             } | |
|         } | |
|     } | |
|     if (UART1_GetITFlag() == UART_II_RECV_TOUT) | |
|     { | |
|         uint8_t RxData[RxSize] = {0}; | |
|         uint16_t len = UART1_RecvString(RxData); | |
|         for (int j = 0; j < len; ++j) | |
|         { | |
|             if (!Fifo_IsFull(&UartList[1].RxFifo)) | |
|             { | |
|                 Fifo_Push(&UartList[1].RxFifo, RxData[j]); | |
|             } | |
|         } | |
|     } | |
| } |