g_serial_ctx.rx_callback = config->rx_callback;
g_serial_ctx.rx_callback_arg = config->rx_callback_arg;
- /* 配置串口引脚 */
- g_serial_ctx.pin_config.tx_pin = config->tx_pin;
- g_serial_ctx.pin_config.rx_pin = config->rx_pin;
- g_serial_ctx.pin_config.cts_pin = PIN_NONE;
- g_serial_ctx.pin_config.rts_pin = PIN_NONE;
-
- /* 配置串口属性 */
- g_serial_ctx.uart_attr.baud_rate = config->baudrate;
- g_serial_ctx.uart_attr.data_bits = UART_DATA_BIT_8;
- g_serial_ctx.uart_attr.parity = UART_PARITY_NONE;
- g_serial_ctx.uart_attr.stop_bits = UART_STOP_BIT_1;
-
- /* 配置串口缓冲区 */
- g_serial_ctx.buffer_config.rx_buffer_size = 256; // 硬件缓冲区
- g_serial_ctx.buffer_config.rx_buffer = (uint8_t *)osal_kmalloc(256, OSAL_GFP_KERNEL);
- if (!g_serial_ctx.buffer_config.rx_buffer) {
- PPP_DEBUG_ERROR("Failed to allocate UART RX buffer");
- osal_sem_destroy(g_serial_ctx.rx_semaphore);
- osal_mutex_destroy(g_serial_ctx.rx_mutex);
- osal_kfree(g_serial_ctx.rx_buffer);
- osal_kfree(g_serial_ctx.tx_buffer);
- return PPP_ERR_MEMORY;
- }
-
- /* 初始化串口 */
- errcode_t ret = uapi_uart_init(g_serial_ctx.uart_bus,
- &g_serial_ctx.pin_config,
- &g_serial_ctx.uart_attr,
- NULL,
- &g_serial_ctx.buffer_config);
- if (ret != ERRCODE_SUCC) {
- PPP_DEBUG_ERROR("Failed to initialize UART: %d", ret);
- osal_kfree(g_serial_ctx.buffer_config.rx_buffer);
- osal_sem_destroy(g_serial_ctx.rx_semaphore);
- osal_mutex_destroy(g_serial_ctx.rx_mutex);
- osal_kfree(g_serial_ctx.rx_buffer);
- osal_kfree(g_serial_ctx.tx_buffer);
- return PPP_ERR_UART;
- }
-
- /* 注册接收回调 */
- ret = uapi_uart_register_rx_callback(g_serial_ctx.uart_bus,
- UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE,
- 1, // 接收到1个字节就触发回调
- ppp_serial_rx_callback);
- if (ret != ERRCODE_SUCC) {
- PPP_DEBUG_ERROR("Failed to register UART RX callback: %d", ret);
- uapi_uart_deinit(g_serial_ctx.uart_bus);
- osal_kfree(g_serial_ctx.buffer_config.rx_buffer);
- osal_sem_destroy(g_serial_ctx.rx_semaphore);
- osal_mutex_destroy(g_serial_ctx.rx_mutex);
- osal_kfree(g_serial_ctx.rx_buffer);
- osal_kfree(g_serial_ctx.tx_buffer);
- return PPP_ERR_UART;
- }
-
-- /* 初始化缓冲区状态 */
-- g_serial_ctx.rx_head = 0;
-- g_serial_ctx.rx_tail = 0;
-- g_serial_ctx.rx_count = 0;
--
-- /* 清零统计信息 */
-- g_serial_ctx.tx_bytes = 0;
-- g_serial_ctx.rx_bytes = 0;
-- g_serial_ctx.tx_errors = 0;
-- g_serial_ctx.rx_errors = 0;
--
-- g_serial_ctx.initialized = true;
-- PPP_DEBUG_INFO("PPP serial initialized successfully");
--
-- return PPP_ERR_OK;
- }
- void uart_read_data_int_handler(const void *buffer, uint16_t length, bool error)
- {
- ppp_serial_rx_callback(buffer, length,error);
--}
-
--/**
-- * @brief 反初始化PPP串口
-- */
--void ppp_serial_deinit(void)
--{
-- if (!g_serial_ctx.initialized) {
-- return;
-- }
--
-- PPP_DEBUG_INFO("Deinitializing PPP serial");
--
- /* 关闭SIO接口 */
- if (g_serial_ctx.sio_fd != NULL) {
- sio_close(g_serial_ctx.sio_fd);
- g_serial_ctx.sio_fd = NULL;
- /* 反初始化串口 */
- uapi_uart_deinit(g_serial_ctx.uart_bus);
-
- /* 释放资源 */
- if (g_serial_ctx.buffer_config.rx_buffer) {
- osal_kfree(g_serial_ctx.buffer_config.rx_buffer);
- }
-
- if (g_serial_ctx.rx_semaphore) {
- osal_sem_destroy(g_serial_ctx.rx_semaphore);
- }
-
- if (g_serial_ctx.rx_mutex) {
- osal_mutex_destroy(g_serial_ctx.rx_mutex);
- }
-
- if (g_serial_ctx.rx_buffer) {
- osal_kfree(g_serial_ctx.rx_buffer);
- }
-
- if (g_serial_ctx.tx_buffer) {
- osal_kfree(g_serial_ctx.tx_buffer);
}
memset_s(&g_serial_ctx, sizeof(g_serial_ctx), 0, sizeof(g_serial_ctx));