Best aproach to share/switch a UART?

Hi there, right now I'm working with an application that allows me to collect data via UART from several sensors (two), and I also I have a SIM module that allows me to communicate via UART.
But I'm facing the problem that I'm just using a single UART to achieve all of these tasks.
But in some cases, I can't use the UART when I need it at a specific moment because it has been acquired for another task.
Then, what could be the best approach to sharing or switching a single UART beyond more a timeout?
Whatever advice you leave, it'll be received.
Thanks.
  • Try this new fixed code where I tried to add the mutex like I suggested to your code. It also makes the logic simpler. I have not compiled it but want to show you the logic how it is done. You are very capable of fixing the compiler issues.

    For some reason, I am unable to upload files, So I will try to upload files in a different comment

  • static void rx_tx_function(void *pvParameter) {
        while (1) {
            DISPLAY_MODULE_UART_getCurrentDisplayedWindow(pCurrentWindow, pNumberCurrentWindow, pAll_OK);
            
            if (all_OK) {
                SEGGER_RTT_printf(0, "[INFO] - from->[%s()]->Current_Window = [%s]\n", __FUNCTION__, currentWindow);
                
                if (numberCurrentWindow != SKIPPED_WINDOW) {
                    SEGGER_RTT_printf(0, "[INFO] - from->[%s()]->NumberCurrentWindow = [%d]\n", __FUNCTION__, numberCurrentWindow);
                } else {
                    SEGGER_RTT_printf(0, "[INFO] - from->[%s()]->Skipped: [%s]\n", currentWindow);
                }
            } else {
                SEGGER_RTT_printf(0, "[INFO] - from->[%s()]->Handle timeoutTicks OR Failed to extract Current Window.\n", __FUNCTION__);
            }
            
            vTaskDelay(pdMS_TO_TICKS(10));
        }
    }
    
    static void rx_task_function(void *pvParameter) {
        DISPLAY_MODULE_UART_printCurrentState(currentState.currentState);
        
        if (currentState.currentState == STATE_WHICH_BUTTON_WAS_PRESS) {
            DISPLAY_MODULE_UART_processSTATE_WHICH_BUTTON_WAS_PRESS(ptrCurrentState, ptrCountWindows);
            
            if (countWindows.countTimeWait_WIN_8 == 3 || countWindows.countTimeWait_WIN_10 == 3) {
                SEGGER_RTT_printf(0, "[INFO] - from->[%s()]->countWindows.countTimeWait_WIN_8 = [%d]\n", __FUNCTION__, countWindows.countTimeWait_WIN_8);
                SEGGER_RTT_printf(0, "[INFO] - from->[%s()]->countWindows.countTimeWait_WIN_10 = [%d]\n", __FUNCTION__, countWindows.countTimeWait_WIN_10);
                
                currentState.currentState = STATE_GOTO_HOME_PAGE;
                countWindows.countTimeWait_WIN_8 = 0;
                countWindows.countTimeWait_WIN_10 = 0;
            }
        } else {
            DISPLAY_MODULE_UART_callFSM(ptrCurrentState);
        }
        
        SEGGER_RTT_printf(0, ">>>>>>>>>> [INFO] - from->[%s()]->EXIT\n", __FUNCTION__);
        vTaskDelay(pdMS_TO_TICKS(10));
    }
    
    // Define the mutex for UART access
    static SemaphoreHandle_t uart_mutex;
    
    void init_uart_mutex() {
        uart_mutex = xSemaphoreCreateMutex();
        if (uart_mutex == NULL) {
            SEGGER_RTT_printf(0, "Failed to create UART mutex\n");
        }
    }
    
    static void rx_tx_function(void *pvParameter) {
        while (1) {
            // Try to acquire the mutex before UART operation
            if (xSemaphoreTake(uart_mutex, portMAX_DELAY) == pdTRUE) {
                // Perform UART operations
                DISPLAY_MODULE_UART_getCurrentDisplayedWindow(pCurrentWindow, pNumberCurrentWindow, pAll_OK);
    
                if (all_OK) {
                    SEGGER_RTT_printf(0, "[INFO] - from->[%s()]->Current_Window = [%s]\n", __FUNCTION__, currentWindow);
                    if (numberCurrentWindow != SKIPPED_WINDOW) {
                        SEGGER_RTT_printf(0, "[INFO] - from->[%s()]->NumberCurrentWindow = [%d]\n", __FUNCTION__, numberCurrentWindow);
                    } else {
                        SEGGER_RTT_printf(0, "[INFO] - from->[%s()]->Skipped: [%s]\n", currentWindow);
                    }
                } else {
                    SEGGER_RTT_printf(0, "[INFO] - from->[%s()]->Handle timeoutTicks OR Failed to extract Current Window.\n", __FUNCTION__);
                }
    
                // Release the mutex after UART operation
                xSemaphoreGive(uart_mutex);
            }
    
            vTaskDelay(pdMS_TO_TICKS(10));
        }
    }
    
    static void rx_task_function(void *pvParameter) {
        while (1) {
            // Try to acquire the mutex before UART operation
            if (xSemaphoreTake(uart_mutex, portMAX_DELAY) == pdTRUE) {
                // Perform UART operations
                DISPLAY_MODULE_UART_printCurrentState(currentState.currentState);
    
                if (currentState.currentState == STATE_WHICH_BUTTON_WAS_PRESS) {
        
    
    0724.diff.patch

    Files attached

Related