From ae079c1fc5d990ba55714d4b3a51b19f96edaec4 Mon Sep 17 00:00:00 2001
From: WXK <287788329@qq.com>
Date: 星期四, 24 四月 2025 16:01:43 +0800
Subject: [PATCH] 改为中断来低电平触发发送当前扫描数据,3s内扫描不到的会退出,串口来55 AA 75 70 64 61 74 65,进入升级模式

---
 01_SDK/modules/hal/panchip/panplat/pan1070/bsp/peripheral/inc/pan_hal_uart.h |  366 ++++++++++++++++++++++++++++++++-------------------
 1 files changed, 229 insertions(+), 137 deletions(-)

diff --git a/01_SDK/modules/hal/panchip/panplat/pan1070/bsp/peripheral/inc/pan_hal_uart.h b/01_SDK/modules/hal/panchip/panplat/pan1070/bsp/peripheral/inc/pan_hal_uart.h
index d66d985..b9fca1a 100644
--- a/01_SDK/modules/hal/panchip/panplat/pan1070/bsp/peripheral/inc/pan_hal_uart.h
+++ b/01_SDK/modules/hal/panchip/panplat/pan1070/bsp/peripheral/inc/pan_hal_uart.h
@@ -6,7 +6,6 @@
 * @brief    Panchip series UART (Universal Asynchronous Receiver-Transmitter) HAL header file.
 * @note     Copyright (C) 2023 Panchip Technology Corp. All rights reserved.
 *****************************************************************************/
-
 #ifndef __PAN_HAL_UART_H__
 #define __PAN_HAL_UART_H__
 
@@ -20,13 +19,15 @@
 
 typedef enum 
 {
-    HAL_UART_CB_TX_EMPTY       = 0UL, /**< @brief Callback when the transmit buffer is empty. */
-    HAL_UART_CB_TX_FULL,              /**< @brief Callback when the transmit buffer is full. */
-    HAL_UART_CB_LINE_ERROR,           /**< @brief Callback for receiver line errors. */
-    HAL_UART_CB_TX_FINISH,            /**< @brief Callback when the transmit operation is finished. */
-    HAL_UART_CB_MODEM,                /**< @brief Callback for modem status. */
-    HAL_UART_CB_MAX                   /**< @brief Sentinel value. */
-} UART_Callback_Type_Opt;
+    HAL_UART_EVT_TX_FINISH       = 0UL, /**< @brief Event when the transmit operation is finished. */
+    HAL_UART_EVT_RX_FINISH,             /**< @brief Event when the receive operation is finished. */
+    HAL_UART_EVT_CONTI_RX_TIMEOUT,      /**< @brief Event when the continuous receive timeout. */
+    HAL_UART_EVT_CONTI_RX_BUFF_FULL,    /**< @brief Event when the continuous receive buffer full. */
+    HAL_UART_EVT_RX_ERR_PARITY,         /**< @brief Event when parity error occurs on the rx line. */
+    HAL_UART_EVT_RX_ERR_FRAME,          /**< @brief Event when frame error occurs on the rx line. */
+    HAL_UART_EVT_RX_ERR_OVERRUN,        /**< @brief Event when fifo overrun error occurs on the rx line. */
+    HAL_UART_EVT_MAX                    /**< @brief Sentinel value. */
+} HAL_UART_EventOpt;
 
 typedef enum
 {
@@ -35,20 +36,30 @@
     HAL_UART_INT_LINE_STATUS    = 0x04, /**< @brief Receiver line status interrupt. */
     HAL_UART_INT_MODEM_STATUS   = 0x08, /**< @brief Modem status interrupt. */
     HAL_UART_INT_ALL            = 0x0f, /**< @brief All interrupts. */
-    HAL_UART_INT_DISABLE = 0x00 /**< @brief Disable interrupts. */
-} UART_Interrupt_Mode_Opt;
+    HAL_UART_INT_DISABLE        = 0x00  /**< @brief Disable interrupts. */
+} HAL_UART_IntModeOpt;
 
-// Configure UART software buffer size
-#define UART_RX_BUF_SIZE        256
-#define UART_TX_BUF_SIZE        256
+typedef enum 
+{
+    HAL_UART_TX_FIFO_EMPTY              = UART_TX_FIFO_EMPTY,
+    HAL_UART_TX_FIFO_TWO_CHARS          = UART_TX_FIFO_TWO_CHARS,
+    HAL_UART_TX_FIFO_QUARTER_FULL       = UART_TX_FIFO_QUARTER_FULL,
+    HAL_UART_TX_FIFO_HALF_FULL          = UART_TX_FIFO_HALF_FULL
+} HAL_UART_TxTrigOpt;
 
-extern uint8_t uartRxBuffer[UART_RX_BUF_SIZE];
-extern uint8_t uartTxBuffer[UART_TX_BUF_SIZE];
-extern volatile uint32_t uartRxBufIdx;
-extern uint32_t uartTxBufIdx;
-extern volatile bool uartTxDone;
-extern volatile bool uartAddrRcvd;
-extern volatile bool uartRxIntFlag;
+typedef enum
+{
+    HAL_UART_RX_FIFO_ONE_CHAR           = UART_RX_FIFO_ONE_CHAR,
+    HAL_UART_RX_FIFO_QUARTER_FULL       = UART_RX_FIFO_QUARTER_FULL,
+    HAL_UART_RX_FIFO_HALF_FULL          = UART_RX_FIFO_HALF_FULL,
+    HAL_UART_RX_FIFO_TWO_LESS_THAN_FULL = UART_RX_FIFO_TWO_LESS_THAN_FULL
+} HAL_UART_RxTrigOpt;
+
+typedef enum
+{
+    HAL_UART_DMA_TX,                    /**< @brief DMA Direction for UART Tx. */
+    HAL_UART_DMA_RX                     /**< @brief DMA Direction for UART Rx. */
+} HAL_UART_DmaDirOpt;
 
 /**
  * @brief UART Format Options Enum Definition.
@@ -98,16 +109,43 @@
     HAL_UART_FMT_8_S_2       = 0x3f,     // 8 data bits, space parity, 2 stop bits
     HAL_UART_FMT_8_M_1       = 0x2b,     // 8 data bits, mark parity, 1 stop bit
     HAL_UART_FMT_8_M_2       = 0x2f,     // 8 data bits, mark parity, 2 stop bits
-} UART_Format_Opt;
+} HAL_UART_FormatOpt;
+
+///**
+//  * @brief HAL UART Event Control type definition
+//  * @note  HAL UART Event Control type value aims to identify which type of Event
+//  *        should be generated leading to call of the Event Callback.
+//  *        This parameter can be a value of @ref HAL_UART_EvtCtrlOpt_Values :
+//  *           HAL_UART_TX_FINISH_EVT_EN                 = BIT0,
+//  *           HAL_UART_RX_FINISH_EVT_EN                 = BIT1,
+//  *           HAL_UART_RX_ERROR_EVT_EN                  = BIT2,
+//  */
+//typedef uint8_t HAL_UART_EvtCtrlOpt;
+
+///** @defgroup HAL_UART_EvtCtrlOpt_Values  UART Event Control Values
+//  * @{
+//  */
+//#define HAL_UART_TX_FINISH_EVT_EN               BIT0             /*!< Specifies this control flag to enable UART Tx Finish Event */
+//#define HAL_UART_RX_FINISH_EVT_EN               BIT1             /*!< Specifies this control flag to enable UART Rx Finish Event */
+//#define HAL_UART_RX_ERROR_EVT_EN                BIT2             /*!< Specifies this control flag to enable UART Rx Error Event */
+///**
+//  * @}
+//  */
+
+//struct _HAL_UART_HandleTypeDef;
+
+typedef struct _HAL_UART_HandleTypeDef HAL_UART_HandleTypeDef;
+
+typedef void (*HAL_UART_CallbackFunc)(HAL_UART_HandleTypeDef*, HAL_UART_EventOpt, uint8_t*, uint16_t);
 
 /**
  * @brief  UART Init Structure definition
  */
 typedef struct
 {
-  uint32_t baudRate;      /**< @brief Set the initial baud rate. */
-  UART_Format_Opt format; /**< @brief Set the initial line control parameters. */
-} UART_Init_Opt;
+  uint32_t baudRate;        /**< @brief Set the initial baud rate. */
+  HAL_UART_FormatOpt format;    /**< @brief Set the initial line control parameters. */
+} HAL_UART_InitTypeDef;
 
 /**
  * @brief UART Interrupt Options Structure Definition.
@@ -117,46 +155,51 @@
  */
 typedef struct
 {
-    bool switchFlag;                      /**< @brief Enables or disables the interrupt. True to enable, false to disable. */
-    uint8_t interruptMode;     		/**< @brief Specifies the interrupt mode or triggering condition. */
-    UART_Callback_Type_Opt callbackTyp; /**< @brief Specifies the type or category of the callback function. */
-    UART_CallbackFunc callbackFunc;   /**< @brief Pointer to the callback function to be executed when the specified interrupt event occurs. */
-} UART_Interrupt_Opt;
+//    HAL_UART_EvtCtrlOpt eventCtrl;      /**< @brief Specifies which interrupt event(s) should be generated for related callback function. */
+    HAL_UART_TxTrigOpt txTrigLevel;     /**< @brief Specifies the Tx interrupt trigger level. */
+    HAL_UART_RxTrigOpt rxTrigLevel;     /**< @brief Specifies the Rx interrupt trigger level. */
+    IRQn_Type IRQn;                     /**< @brief Specifies the IRQ number associated with current UART module. */
+    uint8_t IrqPriority;                /**< @brief Specifies priority of current UART IRQ, value can be 0 ~ 3. */
+    __IO bool continuousRxMode;         /*!< Indicates UART Rx is in continuous interrupt transfer mode. */
+    HAL_UART_CallbackFunc callbackFunc; /**< @brief Pointer to the callback function to be executed when the specified interrupt event occurs. */
+} HAL_UART_IntTypeDef;
 
 /**
  * @brief  UART handle Structure definition
  */
-typedef struct
+struct _HAL_UART_HandleTypeDef
 {
-    UART_T *pUartx; /*!< UART registers base address. */
+    UART_T *pUartx;                     /*!< UART registers base address. */
 
-    UART_Init_Opt initObj; /*!< UART communication parameters. */
+    HAL_UART_InitTypeDef initObj;       /*!< UART communication parameters. */
 
-    UART_Interrupt_Opt interruptObj;
+    HAL_UART_IntTypeDef interruptObj;   /*!< UART interrupt communication parameters. */
 
-    uint8_t *pTxBuffPtr; /*!< Pointer to UART Tx transfer Buffer. */
-    uint16_t txXferSize; /*!< UART Tx Transfer size. */
-    __IO uint16_t txXferCount; /*!< UART Tx Transfer Counter. */
+    uint8_t *pTxBuffPtr;                /*!< Pointer to UART Tx transfer Buffer. */
+    uint16_t txXferSize;                /*!< UART Tx Transfer size. */
+    __IO uint16_t txXferCount;          /*!< UART Tx Transfer Counter. */
 
-    uint8_t *pRxBuffPtr; /*!< Pointer to UART Rx transfer Buffer. */
-    uint16_t rxXferSize; /*!< UART Rx Transfer size. */
-    __IO uint16_t rxXferCount; /*!< UART Rx Transfer Counter. */
+    uint8_t *pRxBuffPtr;                /*!< Pointer to UART Rx transfer Buffer. */
+    uint16_t rxXferSize;                /*!< UART Rx Transfer size. */
+    __IO uint16_t rxXferCount;          /*!< UART Rx Transfer Counter. */
 
-    IRQn_Type IRQn;                  /**< IRQ number associated with the UART. */
+    __IO bool isTxBusy;                 /*!< Indicates UART is busy transmitting data. */
+    __IO bool isRxBusy;                 /*!< Indicates UART is busy receiving data. */
 
-    UART_CallbackFunc rxIntCallback; /**< Callback function for UART receive interrupts. */
-    UART_CallbackFunc txIntCallback; /**< Callback function for UART transmit interrupts. */
+    uint8_t txDmaCh;                    /*!< DMA Channel for UART Tx */
+    uint8_t rxDmaCh;                    /*!< DMA Channel for UART Rx */
 
-    uint32_t dmaSrc;                 /**< DMA source channel for UART data transfer. */
-    uint32_t dmaDst;                 /**< DMA destination channel for UART data transfer. */
-    
-    __IO uint32_t errorCode;         /**< UART error code, indicating any encountered errors. */
-} UART_HandleTypeDef;
+    uint32_t dmaSrc;                    /**< DMA source channel for UART data transfer. */
+    uint32_t dmaDst;                    /**< DMA destination channel for UART data transfer. */
 
+//    __IO uint32_t errorCode;            /**< UART error code, indicating any encountered errors. */
+};
+
+#define PAN_HAL_UART_INST_COUNT     2
 #define UART0_OBJ UART_Handle_Array[0]    /**< UART handle for UART0. */
 #define UART1_OBJ UART_Handle_Array[1]    /**< UART handle for UART1. */
 
-extern UART_HandleTypeDef UART_Handle_Array[2];  /**< Array of UART handles for multiple UART instances. */
+extern HAL_UART_HandleTypeDef UART_Handle_Array[PAN_HAL_UART_INST_COUNT];  /**< Array of UART handles for multiple UART instances. */
 
 /**
  * @brief Initialize the UART peripheral.
@@ -164,86 +207,11 @@
  * This function initializes the UART peripheral with the specified baud rate and format.
  * It configures the UART's line control parameters, including data bits, stop bits, and parity.
  *
- * @param[in] uart Pointer to the UART handle structure.
+ * @param[in] pUart Pointer to the UART handle structure.
  *
- * @return true if initialization is successful, false otherwise.
+ * @return HAL Status
  */
-bool HAL_UART_Init(UART_HandleTypeDef *pUart);
-
-/**
- * @brief Send data using UART.
- *
- * This function sends data over UART without interrupts. It waits until the UART's
- * transmitter holding register is empty before sending each byte.
- *
- * @param[in] uart Pointer to the UART handle structure.
- * @param[in] Buf  Pointer to the data buffer to be transmitted.
- * @param[in] Size Size of the data to be transmitted.
- */
-void HAL_UART_SendData(UART_HandleTypeDef *pUart, uint8_t *pBuf, size_t size);
-
-/**
- * @brief Receive data using UART.
- *
- * This function receives data over UART without interrupts. It continuously polls
- * the UART's data register and stores received bytes in the specified buffer.
- *
- * @param[in] uart     Pointer to the UART handle structure.
- * @param[in] Buf      Pointer to the data buffer to store received data.
- * @param[in] Size     Size of the data to be received.
- * @param[in] Timeout  Timeout value for receiving data.
- */
-void HAL_UART_ReceiveData(UART_HandleTypeDef *pUart, uint8_t *pBuf, size_t size, uint32_t timeout);
-
-/**
- * @brief Transmit data using UART with interrupt.
- *
- * This function initializes the UART transmission with interrupt and sets up the required parameters.
- *
- * @param[in] uart     Pointer to the UART handle structure.
- * @param[in] Buf      Pointer to the data buffer to be transmitted.
- * @param[in] Size     Size of the data to be transmitted.
- * @param[in] Callback Callback function to be called when the transmission is complete.
- */
-void HAL_UART_SendData_INT(UART_HandleTypeDef *pUart, uint8_t *pBuf, size_t size, UART_CallbackFunc callback);
-
-/**
- * @brief Receive data using UART with interrupt.
- *
- * This function initializes the UART reception with interrupt and sets up the required parameters.
- *
- * @param[in] uart     Pointer to the UART handle structure.
- * @param[in] Buf      Pointer to the data buffer to store received data.
- * @param[in] Size     Size of the data to be received.
- * @param[in] Timeout  Timeout value for receiving data.
- * @param[in] Callback Callback function to be called when data is received or a timeout occurs.
- */
-void HAL_UART_ReceiveData_INT(UART_HandleTypeDef *pUart, uint8_t *pBuf, size_t size, uint32_t timeout, UART_CallbackFunc callback);
-
-/**
- * @brief Transmit data using UART with DMA.
- *
- * This function initializes UART transmission with DMA and sets up the required parameters.
- *
- * @param[in] uart     Pointer to the UART handle structure.
- * @param[in] Buf      Pointer to the data buffer to be transmitted.
- * @param[in] Size     Size of the data to be transmitted.
- * @param[in] Callback Callback function to be called when the transmission is complete.
- */
-void HAL_UART_SendData_DMA(UART_HandleTypeDef *pUart, uint8_t *pBuf, size_t size, UART_CallbackFunc callback);
-
-/**
- * @brief Receive data using UART with DMA.
- *
- * This function initializes UART reception with DMA and sets up the required parameters.
- *
- * @param[in] uart     Pointer to the UART handle structure.
- * @param[in] Buf      Pointer to the data buffer to store received data.
- * @param[in] Size     Size of the data to be received.
- * @param[in] Timeout  Timeout value for receiving data.
- * @param[in] Callback Callback function to be called when data is received.
- */
-void HAL_UART_ReceiveData_DMA(UART_HandleTypeDef *pUart, uint8_t *pBuf, size_t size, uint32_t timeout, UART_CallbackFunc callback);
+HAL_Status HAL_UART_Init(HAL_UART_HandleTypeDef *pUart);
 
 /**
  * @brief Initialize UART interrupt settings.
@@ -251,27 +219,151 @@
  * This function configures the UART interrupt settings based on the provided
  * UART handle and the settings specified in the InterruptObj structure.
  *
- * @param[in] uart Pointer to the UART handle structure.
+ * @param[in] pUart Pointer to the UART handle structure.
+ * @return HAL Status
  */
-void HAL_UART_Init_INT(UART_HandleTypeDef *pUart);
+HAL_Status HAL_UART_Init_INT(HAL_UART_HandleTypeDef *pUart);
 
 /**
- * @brief Interrupt handler for UART0.
+ * @brief Initialize UART interrupt settings.
  *
- * This function is called when an interrupt occurs for UART0. It processes
- * the interrupt by calling the UART_HandleProc function for UART0.
+ * This function configures the UART interrupt settings based on the provided
+ * UART handle and the settings specified in the InterruptObj structure.
+ *
+ * @param[in] pUart Pointer to the UART handle structure.
+ * @param[in] dmaDir DMA Direction for UART Tx or Rx.
+ * @param[in] callback Pointer to the UART DMA Tx/Rx handle due to dmaDir.
+ * @return HAL Status
  */
-void UART0_IRQHandler(void);
+HAL_Status HAL_UART_Init_DMA(HAL_UART_HandleTypeDef *pUart, HAL_UART_DmaDirOpt dmaDir, UART_CallbackFunc callback);
 
 /**
- * @brief Interrupt handler for UART1.
+ * @brief Send data using UART.
  *
- * This function is called when an interrupt occurs for UART1. It processes
- * the interrupt by calling the UART_HandleProc function for UART1.
+ * This function sends data over UART without interrupts. It waits until the UART's
+ * transmitter holding register is empty before sending each byte.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @param[in] pBuf      Pointer to the data buffer to be transmitted.
+ * @param[in] size      Size of the data to be transmitted.
+ * @param[in] timeout   Timeout for sending data in millisecond, can be a number or HAL_TIME_FOREVER.
+ * @return HAL Status
  */
-void UART1_IRQHandler(void);
+HAL_Status HAL_UART_SendData(HAL_UART_HandleTypeDef *pUart, uint8_t *pBuf, uint16_t size, uint32_t timeout);
 
-extern void HAL_DelayMs(uint32_t t);
+/**
+ * @brief Receive data using UART.
+ *
+ * This function receives data over UART without interrupts. It continuously polls
+ * the UART's data register and stores received bytes in the specified buffer.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @param[in] pBuf      Pointer to the data buffer to store received data.
+ * @param[in] size      Size of the data to be received.
+ * @param[in] timeout   Timeout for receiving data in millisecond, can be a number or HAL_TIME_FOREVER.
+ * @return HAL Status
+ */
+HAL_Status HAL_UART_ReceiveData(HAL_UART_HandleTypeDef *pUart, uint8_t *pBuf, uint16_t size, uint32_t timeout);
+
+/**
+ * @brief Transmit data using UART with interrupt.
+ *
+ * This function initializes the UART transmission with interrupt and sets up the required parameters.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @param[in] pBuf      Pointer to the data buffer to be transmitted.
+ * @param[in] size      Size of the data to be transmitted.
+ * @return HAL Status
+ */
+HAL_Status HAL_UART_SendData_INT(HAL_UART_HandleTypeDef *pUart, uint8_t *pBuf, uint16_t size);
+
+/**
+ * @brief Receive data using UART with interrupt.
+ *
+ * This function initializes the UART reception with interrupt and sets up the required parameters.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @param[in] pBuf      Pointer to the data buffer to store received data.
+ * @param[in] size      Size of the data to be received.
+ * @return HAL Status
+ */
+HAL_Status HAL_UART_ReceiveData_INT(HAL_UART_HandleTypeDef *pUart, uint8_t *pBuf, uint16_t size);
+
+/**
+ * @brief Receive data continuously using UART with interrupt.
+ *
+ * This function initializes the UART continous reception with interrupt and sets up the required parameters.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @param[in] pBuf      Pointer to the data buffer to store received data.
+ * @param[in] bufSize   Size of the data buffer for receiving.
+ * @return HAL Status
+ */
+HAL_Status HAL_UART_ReceiveDataContinuously_INT(HAL_UART_HandleTypeDef *pUart, uint8_t *pBuf, uint16_t bufSize);
+
+/**
+ * @brief Abort transmitting data using UART with interrupt.
+ *
+ * This function aborts the UART transmission with interrupt.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @return HAL Status
+ */
+HAL_Status HAL_UART_SendDataAbort_INT(HAL_UART_HandleTypeDef *pUart);
+
+/**
+ * @brief Abort receiving data using UART with interrupt.
+ *
+ * This function aborts the UART receiving with interrupt.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @return HAL Status
+ */
+HAL_Status HAL_UART_ReceiveDataAbort_INT(HAL_UART_HandleTypeDef *pUart);
+
+/**
+ * @brief Transmit data using UART with DMA.
+ *
+ * This function initializes UART transmission with DMA and sets up the required parameters.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @param[in] pBuf      Pointer to the data buffer to be transmitted.
+ * @param[in] size      Size of the data to be transmitted.
+ * @return HAL Status
+ */
+HAL_Status HAL_UART_SendData_DMA(HAL_UART_HandleTypeDef *pUart, uint8_t *pBuf, uint16_t size);
+
+/**
+ * @brief Receive data using UART with DMA.
+ *
+ * This function initializes UART reception with DMA and sets up the required parameters.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @param[in] pBuf      Pointer to the data buffer to store received data.
+ * @param[in] size      Size of the data to be received.
+ * @return HAL Status
+ */
+HAL_Status HAL_UART_ReceiveData_DMA(HAL_UART_HandleTypeDef *pUart, uint8_t *pBuf, uint16_t size);
+
+/**
+ * @brief Abort transmitting data using UART with DMA.
+ *
+ * This function aborts the UART transmission with DMA.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @return HAL Status
+ */
+HAL_Status HAL_UART_SendDataAbort_DMA(HAL_UART_HandleTypeDef *pUart);
+
+/**
+ * @brief Abort receiving data using UART with DMA.
+ *
+ * This function aborts the UART receiving with DMA.
+ *
+ * @param[in] pUart     Pointer to the UART handle structure.
+ * @return HAL Status
+ */
+HAL_Status HAL_UART_ReceiveDataAbort_DMA(HAL_UART_HandleTypeDef *pUart);
 
 /** @} */ // end of group 
 #endif

--
Gitblit v1.9.3