OSAL
====

Introduction
------------

The OSAL library provides operating system abstraction layer API sample
implementations that are required for processor SDK drivers. Scope of
APIs supported in the module is limited to support OS services required
for RTOS or bare metal drivers

-  Supported Operating Systems

   -  FreeRTOS
   -  SafeRTOS
   -  Non-OS (Baremetal)

-  Supported SOCs

   -  Please refer to component release notes

User Interface
--------------

For details about individual fields of this library structure, see the
PDK doxygen documentation / `OSAL API Guide <../../../apiguide/j721e/html/group__DRV__OSAL__MODULE.html>`_

APIs
^^^^

Porting of OSAL component will enable extending PDK components to
additional OS environment. Reference top level API header file 
includes:

::

    #include <ti/osal/osal.h>

The following table shows which all OSAL modules are supported for FreeRTOS and Non-OS.
Using OSAL APIs that are not supported for an OS will result in linking errors.

+---------------------------------+--------------------+-------------------+-------------------+
|           OSAL Module           |      FreeRTOS      |      SafeRTOS     | Non-OS(Baremetal) |
+=================================+====================+===================+===================+
| Tasks (TaskP)                   |         YES        |        YES        |         NO        |
+---------------------------------+--------------------+-------------------+-------------------+
| Semaphores (SempahoreP)         |         YES        |        YES        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| HW Interrupts (HwiP)            |         YES        |        YES        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| HW Timers (TimerP)              |         YES        |        YES        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| Cache Ops (CahceP)              |         YES        |        YES        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| Cycle profiler (CycleProfilerP) |         YES        |        YES        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| SW Timer (ClockP)               |         YES        |        YES        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| Events (EventP)                 |         YES        |        YES        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| SW Mailbox (MailboxP)           |         YES        |        YES        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| Mutex (MutexP)                  |         YES        |        YES        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| Queue (QueueP)                  |         YES        |         NO        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| Arbitrary Heap (HeapP)          |         YES        |         NO        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| Task/CPU Load (LoadP)           |         YES        |         NO        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+
| SW Interrupts (SwiP)            |         NO         |         NO        |        YES        |
+---------------------------------+--------------------+-------------------+-------------------+

In case of FreeRTOS/Non-OS, static memory allocation is done inside the osal library for **all** the modules.

The static memory allocation for SemaphoreP and HwiP are scalable if needed by the
application users, especially when the create APIs fail to create the SemaphoreP and HwiP handles. (Indicated by NULL handle values).

The application can provide a continuous segment of external memory for SemaphoreP as below.

1. Get the current OSAL attributes

::

    ...
    osal_ret = Osal_getHwAttrs(&hwAttrs);

2. Set the continuous external memory block address and size

::

    ...
    hwAttrs.extSemaphorePBlock.base = (uintptr_t) &semPMemBlock[0];
    ...
    hwAttrs.extSemaphorePBlock.size = SEMP_BLOCK_SIZE;
    ...
    ctrlBitMap                      = OSAL_HWATTR_SET_SEMP_EXT_BASE;
    ...
    osal_ret = Osal_setHwAttrs(ctrlBitMap, &hwAttrs);

The application can provide a continuous segment of external memory for HwiP as below.

1. Get the current OSAL attributes

::

    ...
    osal_ret = Osal_getHwAttrs(&hwAttrs);

2. Set the continuous external memory block address and size

::

    ...
    hwAttrs.extSemaphorePBlock.base = (uintptr_t) &hwiPMemBlock[0];
    ...
    hwAttrs.extSemaphorePBlock.size = HWIP_BLOCK_SIZE;
    ...
    ctrlBitMap                      = OSAL_HWATTR_SET_HWIP_EXT_BASE;
    ...
    osal_ret = Osal_setHwAttrs(ctrlBitMap, &hwAttrs);

Application Integration Details
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#. **Baremetal or Non-OS Use case**

    - Reference library path to be included in application build environment:

    ::

       <ti/osal/lib/nonos/[soc]/[[cpu]/>


#. **FreeRTOS based application**

    - Reference library path to be included in application build environment:

    ::

      <ti/osal/lib/freertos/[soc]/[[cpu]/>

#. **SafeRTOS based application**

    - Reference library path to be included in application build environment:

    ::

      <ti/osal/lib/safertos/[soc]/[[cpu]/>

RTOS Tick-Timer Allocation
^^^^^^^^^^^^^^^^^^^^^^^^^^

For FreeRTOS and SafeRTOS, the below table summarizes the various timers, that are used as tick timers on various cores. Applications
must ensure to not use these timers. if used, this may lead to unexpected behavior, due to clash of timers.

.. ifconfig:: CONFIG_family in ('jacinto')

    +------------+------------+------------+-------------+-------------+-------------+-------------+------------+------------+------------+
    |            |   mcu1_0   |   mcu1_1   |   mcu2_0    |   mcu2_1    |   mcu3_0    |   mcu3_1    |   c7x_1    |  c66xdsp_1 | c66xdsp_2  |
    +============+============+============+=============+=============+=============+=============+============+============+============+
    |  FreeRTOS  |  MCU DMT1  |  MCU DMT2  |    DMT12    |    DMT13    |    DMT14    |    DMT15    |    DMT2    |    DMT0    |    DMT1    |
    +------------+------------+------------+-------------+-------------+-------------+-------------+------------+------------+------------+
    |  SafeRTOS  |  MCU DMT1  |  MCU DMT2  |    DMT12    |    DMT13    |    DMT14    |    DMT15    |    DMT2    |    DMT0    |    DMT1    |
    +------------+------------+------------+-------------+-------------+-------------+-------------+------------+------------+------------+

.. ifconfig:: CONFIG_family in ('j7200')

    +------------+------------+------------+------------+------------+
    |            |   mcu1_0   |   mcu1_1   |   mcu2_0   |   mcu2_1   |
    +============+============+============+============+============+
    |  FreeRTOS  |  MCU DMT1  |  MCU DMT2  |    DMT0    |    DMT1    |
    +------------+------------+------------+------------+------------+
    |  SafeRTOS  |  MCU DMT1  |  MCU DMT2  |    DMT0    |    DMT1    |
    +------------+------------+------------+------------+------------+

.. ifconfig:: CONFIG_family in ('j721s2')

    +------------+------------+------------+------------+------------+------------+------------+------------+------------+
    |            |   mcu1_0   |   mcu1_1   |   mcu2_0   |   mcu2_1   |   mcu3_0   |   mcu3_1   |   c7x_1    |    c7x_2   |
    +============+============+============+============+============+============+============+============+============+
    | FreeRTOS   |  MCU DMT1  |  MCU DMT2  |    DMT2    |    DMT3    |    DMT4    |    DMT5    |   DMT0     |    DMT0    |
    +------------+------------+------------+------------+------------+------------+------------+------------+------------+
    | SafeRTOS   |  MCU DMT1  |  MCU DMT2  |    DMT2    |    DMT3    |    DMT4    |    DMT5    |   DMT0     |    DMT0    |
    +------------+------------+------------+------------+------------+------------+------------+------------+------------+

.. ifconfig:: CONFIG_family in ('j784s4')

    +------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
    |            |   mcu1_0   |   mcu1_1   |   mcu2_0   |   mcu2_1   |   mcu3_0   |   mcu3_1   |   mcu4_0   |   mcu4_1   |   c7x_1    |    c7x_2   |   c7x_3    |   c7x_4    |
    +============+============+============+============+============+============+============+============+============+============+============+============+============+
    | FreeRTOS   |  MCU DMT1  |  MCU DMT2  |    DMT4    |   DMT5     |    DMT6    |    DMT7    |    DMT8    |   DMT9     |    DMT0    |     DMT0   |   DMT0     |    DMT0    |
    +------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
    | SafeRTOS   |  MCU DMT1  |  MCU DMT2  |    DMT4    |   DMT5     |    DMT6    |    DMT7    |    DMT8    |   DMT9     |    DMT0    |     DMT0   |   DMT0     |    DMT0    |
    +------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+

Application
------------

Examples
^^^^^^^^

.. rubric:: OSAL Unit Test
   :name: osal-unit-test

**Application Name**

+-----------+-----------------------+------------------------+------------------------+
|  Example  |        FreeRTOS       |         SafeRTOS       |    Non-OS(Baremetal)   |
+===========+=======================+========================+========================+
| Unit Test | OSAL_TestApp_freertos | OSAL_TestApp_safertos  | OSAL_Baremetal_TestApp |
+-----------+-----------------------+------------------------+------------------------+

**Description**

The unit testapp tests all the supported OSAL modules for an OS 
and prints output on UART console.

**Expected Results**

The output for each of the tests (Hwi, Timer, Delay, Semaphores etc) can
be seen on the UART console like
::

    <Module> tests have passed.
    
After a successful run following will also be displayed on the UART console.

::

    All tests have passed. 
