SPMA085 June   2022 TM4C1230C3PM , TM4C1230C3PM , TM4C1230D5PM , TM4C1230D5PM , TM4C1230E6PM , TM4C1230E6PM , TM4C1230H6PM , TM4C1230H6PM , TM4C1231C3PM , TM4C1231C3PM , TM4C1231D5PM , TM4C1231D5PM , TM4C1231D5PZ , TM4C1231D5PZ , TM4C1231E6PM , TM4C1231E6PM , TM4C1231E6PZ , TM4C1231E6PZ , TM4C1231H6PGE , TM4C1231H6PGE , TM4C1231H6PM , TM4C1231H6PM , TM4C1231H6PZ , TM4C1231H6PZ , TM4C1232C3PM , TM4C1232C3PM , TM4C1232D5PM , TM4C1232D5PM , TM4C1232E6PM , TM4C1232E6PM , TM4C1232H6PM , TM4C1232H6PM , TM4C1233C3PM , TM4C1233C3PM , TM4C1233D5PM , TM4C1233D5PM , TM4C1233D5PZ , TM4C1233D5PZ , TM4C1233E6PM , TM4C1233E6PM , TM4C1233E6PZ , TM4C1233E6PZ , TM4C1233H6PGE , TM4C1233H6PGE , TM4C1233H6PM , TM4C1233H6PM , TM4C1233H6PZ , TM4C1233H6PZ , TM4C1236D5PM , TM4C1236D5PM , TM4C1236E6PM , TM4C1236E6PM , TM4C1236H6PM , TM4C1236H6PM , TM4C1237D5PM , TM4C1237D5PM , TM4C1237D5PZ , TM4C1237D5PZ , TM4C1237E6PM , TM4C1237E6PM , TM4C1237E6PZ , TM4C1237E6PZ , TM4C1237H6PGE , TM4C1237H6PGE , TM4C1237H6PM , TM4C1237H6PM , TM4C1237H6PZ , TM4C1237H6PZ , TM4C123AE6PM , TM4C123AE6PM , TM4C123AH6PM , TM4C123AH6PM , TM4C123BE6PM , TM4C123BE6PM , TM4C123BE6PZ , TM4C123BE6PZ , TM4C123BH6PGE , TM4C123BH6PGE , TM4C123BH6PM , TM4C123BH6PM , TM4C123BH6PZ , TM4C123BH6PZ , TM4C123FE6PM , TM4C123FE6PM , TM4C123FH6PM , TM4C123FH6PM , TM4C123GE6PM , TM4C123GE6PM , TM4C123GE6PZ , TM4C123GE6PZ , TM4C123GH6PGE , TM4C123GH6PGE , TM4C123GH6PM , TM4C123GH6PM , TM4C123GH6PZ , TM4C123GH6PZ , TM4C123GH6ZXR , TM4C123GH6ZXR , TM4C1290NCPDT , TM4C1290NCPDT , TM4C1290NCZAD , TM4C1290NCZAD , TM4C1292NCPDT , TM4C1292NCPDT , TM4C1292NCZAD , TM4C1292NCZAD , TM4C1294KCPDT , TM4C1294KCPDT , TM4C1294NCPDT , TM4C1294NCPDT , TM4C1294NCZAD , TM4C1294NCZAD , TM4C1297NCZAD , TM4C1297NCZAD , TM4C1299KCZAD , TM4C1299KCZAD , TM4C1299NCZAD , TM4C1299NCZAD , TM4C129CNCPDT , TM4C129CNCPDT , TM4C129CNCZAD , TM4C129CNCZAD , TM4C129DNCPDT , TM4C129DNCPDT , TM4C129DNCZAD , TM4C129DNCZAD , TM4C129EKCPDT , TM4C129EKCPDT , TM4C129ENCPDT , TM4C129ENCPDT , TM4C129ENCZAD , TM4C129ENCZAD , TM4C129LNCZAD , TM4C129LNCZAD , TM4C129XKCZAD , TM4C129XKCZAD , TM4C129XNCZAD , TM4C129XNCZAD

 

  1.   Abstract
  2.   Trademarks
  3. 1Introduction
  4. 2How to Install
    1. 2.1 Update the FreeRTOS Version in the TivaWare Directory
    2. 2.2 Adding FreeRTOS Hardware Driver Files for TM4C LaunchPads
  5. 3Architecture for TM4C FreeRTOS Examples
    1. 3.1 Proper Clock Configuration
    2. 3.2 How to use Hardware Interrupts Alongside the FreeRTOS Kernel
  6. 4Example Project Walkthroughs
    1. 4.1 Download and Import the Examples
    2. 4.2 Kernel Examples
      1. 4.2.1 Example: hello
      2. 4.2.2 Example: notify_example
      3. 4.2.3 Example: queue_example
      4. 4.2.4 Example: semaphore_example
      5. 4.2.5 Example: blinky_queue
      6. 4.2.6 Software Timer Examples
        1. 4.2.6.1 Example: timer_sw_oneshot
        2. 4.2.6.2 Example: timer_sw_periodic
        3. 4.2.6.3 Example: timer_sw_led_counter/timer_sw_rgb
    3. 4.3 ADC Examples
      1. 4.3.1 Example: adc_multi_channel
      2. 4.3.2 Example: adc_timer_trigger
    4. 4.4 Hardware Timer Examples
      1. 4.4.1 Example: timer_hw_oneshot
      2. 4.4.2 Example: timer_hw_periodic
      3. 4.4.3 Example: timer_hw_pwm
    5. 4.5 UART Example
      1. 4.5.1 Example: uart_thread_safe
    6. 4.6 Watchdog Example
      1. 4.6.1 Example: watchdog

Example: uart_thread_safe

This example highlights how to create a thread safe function by using a mutex to ensure that only one task is accessing the resource-sensitive portion of a function at a given time regardless of priorities. A thread safe function assures that it can be safely executed by multiple tasks without any potential corruption of data or resources. A mutex is useful for ensuring thread safety as it is a special type of binary semaphore that is designed to control access to resources which are shared between multiple tasks.

This example creates two tasks that send data to a terminal window through UART interface. One task will use a standard TivaWare DriverLib UART API to send data, while the other will use the TivaWare utility UART standard I/O (uartstdio.c) to send a string with the UARTvprintf API call. A mutex is created by using the xSemaphoreCreateMutex API to ensure the UART peripheral is only accessed by one of these tasks at a given time so each UART message is output completely before the next task is given access. The two tasks are created with different priorities and send their character strings to the terminal window at a random interval.

Without the mutex, the characters from the two tasks would have mixed together resulting in a unreadable message displayed on the terminal window. Using a mutex allows for segments of code, such as using the UART peripheral to send a message, to be guarded from access until the task acquires a token (a mutually exclusive flag) which grants access. Once acquired, the task is able to access and execute the guarded section of code. Since the token can only be acquired by a single task at a given time, this protects the UART peripheral from access until the current task is finished transmitting the message. This protection cannot be bypassed by task priority so even a higher priority task must wait until the token is released. Therefore it is critical that the task releases the mutex token immediately after the execution of the guarded section is concluded.

The example also demonstrates two different methods of how to output data on UART to give users different options. One method is a simple output of a message with minimal execution time. The other method uses a more advanced API that can process receiving variables from the application code to include in the message.