/*******************************************************************************
* @file  rsi_os_none.c
* @brief 
*******************************************************************************
* # License
* <b>Copyright 2020 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#include <rsi_driver.h>
#ifndef RSI_WITH_OS
/*
  Include files
 */

/*==============================================*/
rsi_reg_flags_t rsi_critical_section_entry(void)
{
  rsi_reg_flags_t xflags;
  // hold interrupt status before entering critical section
  // disable interrupts
#ifdef RSI_M4_INTERFACE
  xflags = NVIC_GetIRQEnable((IRQn_Type)M4_ISR_IRQ);
  NVIC_DisableIRQ((IRQn_Type)M4_ISR_IRQ);
#else
  xflags = rsi_hal_critical_section_entry();
#endif

  // return stored interrupt status
  return (xflags);
}
/*==============================================*/
void rsi_critical_section_exit(rsi_reg_flags_t xflags)
{
  // restore interrupts while exiting critical section
  if (xflags != 0) {
#ifdef RSI_M4_INTERFACE
    // restore interrupts while exiting critical section
    NVIC_EnableIRQ((IRQn_Type)M4_ISR_IRQ);
#else
    rsi_hal_critical_section_exit();
#endif
  }
}
/*==============================================*/
void *rsi_virtual_to_physical_address(void *virtual_address)
{
  return (virtual_address);
}
/*==============================================*/
void *rsi_physical_to_virtual_address(void *physical_address)
{
  return (physical_address);
}

/*==============================================*/
rsi_error_t rsi_mutex_create(rsi_mutex_handle_t *mutex)
{
  /* Create dummy mutxe pointer because OS not present */
  RSI_NO_OS_ATOMIC_MUTEX_SET(*mutex, RSI_NO_OS_MUTEX_UNLOCKED);
  return (RSI_ERROR_NONE);
}
/*==============================================*/
rsi_error_t rsi_mutex_lock(volatile rsi_mutex_handle_t *mutex)
{
  while (RSI_NO_OS_ATOMIC_MUTEX_CHECK(*mutex, RSI_NO_OS_MUTEX_LOCKED))
    ;
  RSI_NO_OS_ATOMIC_MUTEX_SET(*mutex, RSI_NO_OS_MUTEX_LOCKED);
  return (RSI_ERROR_NONE);
}
/*==============================================*/
rsi_error_t rsi_mutex_unlock(volatile rsi_mutex_handle_t *mutex)
{
  RSI_NO_OS_ATOMIC_MUTEX_SET(*mutex, RSI_NO_OS_MUTEX_UNLOCKED);
  return (RSI_ERROR_NONE);
}
/*==============================================*/
rsi_error_t rsi_mutex_destroy(rsi_mutex_handle_t *mutex)
{
  UNUSED_PARAMETER(mutex); //This statement is added only to resolve compilation warning, value is unchanged
  return (RSI_ERROR_NONE);
}
/*==============================================*/
rsi_error_t rsi_semaphore_create(rsi_semaphore_handle_t *semaphore, uint32_t count)
{
  *semaphore = count;
  return (RSI_ERROR_NONE);
}

/*==============================================*/
rsi_error_t rsi_semaphore_destroy(rsi_semaphore_handle_t *semaphore)
{
  *semaphore = 0;
  return (RSI_ERROR_NONE);
}
/*==============================================*/
rsi_error_t rsi_semaphore_check_and_destroy(rsi_semaphore_handle_t *semaphore)
{
  if (rsi_semaphore_destroy(semaphore) != RSI_ERROR_NONE) {
    return RSI_ERROR_SEMAPHORE_DESTROY_FAILED;
  }
  return (RSI_ERROR_NONE);
}

/*==============================================*/
rsi_error_t rsi_semaphore_wait(rsi_semaphore_handle_t *semaphore, uint32_t timeout_ms)
{
  volatile uint32_t *event;
  rsi_timer_instance_t timer_instance;
  rsi_driver_cb_t *rsi_driver_cb = global_cb_p->rsi_driver_cb;
  event                          = (uint32_t *)semaphore;
  if (0UL != *event) {
    RSI_ATOMIC_DECREMENT(*event);
    return (RSI_ERROR_NONE);
  }
  if (0UL == timeout_ms) {
    while (0UL == *event) {
      // if any events pending
      // call rsi schedular
      rsi_scheduler(&rsi_driver_cb->scheduler_cb);
    }
    RSI_ATOMIC_DECREMENT(*event);
    return (RSI_ERROR_NONE);
  }
  rsi_init_timer(&timer_instance, timeout_ms);
  while ((0UL != timeout_ms) && (0UL == *event)) {
    // if any events pending
    // call rsi schedular
    rsi_scheduler(&rsi_driver_cb->scheduler_cb);
    if (rsi_timer_expired(&timer_instance)) {
      break;
    }
  }
  if (0UL == *event || (0UL == timeout_ms)) {
    return (RSI_ERROR_TIMEOUT);
  } else {
    RSI_ATOMIC_DECREMENT(*event);
  }
  return (RSI_ERROR_NONE);
}
/*==============================================*/
rsi_error_t rsi_semaphore_post_from_isr(rsi_semaphore_handle_t *semaphore)
{
  volatile uint32_t *event;

  event = (uint32_t *)semaphore;
  RSI_ATOMIC_INCREMENT(*event);
  return (RSI_ERROR_NONE);
}
/*==============================================*/
rsi_error_t rsi_semaphore_post(rsi_semaphore_handle_t *semaphore)
{
  volatile uint32_t *event;

  event = (uint32_t *)semaphore;
  RSI_ATOMIC_INCREMENT(*event);
  return (RSI_ERROR_NONE);
}
/*==============================================*/
rsi_error_t rsi_semaphore_reset(rsi_semaphore_handle_t *semaphore)
{
  volatile uint32_t *event;

  event  = (uint32_t *)semaphore;
  *event = 0;
  return (RSI_ERROR_NONE);
}

#endif

Functions#

rsi_reg_flags_t

Enter critical section.

void
rsi_critical_section_exit(rsi_reg_flags_t xflags)

Exit critical section by restoring interrupts.

void *
rsi_virtual_to_physical_address(void *virtual_address)

Map virtual address to physical address.

void *
rsi_physical_to_virtual_address(void *physical_address)

Map physical address to virtual address.

rsi_error_t
rsi_mutex_create(rsi_mutex_handle_t *mutex)

Create and initialize the mutex.

rsi_error_t
rsi_mutex_lock(volatile rsi_mutex_handle_t *mutex)

Lock the mutex.

rsi_error_t
rsi_mutex_unlock(volatile rsi_mutex_handle_t *mutex)

Unlock the mutex.

rsi_error_t
rsi_mutex_destroy(rsi_mutex_handle_t *mutex)

Destroy the mutex.

rsi_error_t
rsi_semaphore_create(rsi_semaphore_handle_t *semaphore, uint32_t count)

Create and initialize the semaphore instance.

rsi_error_t
rsi_semaphore_destroy(rsi_semaphore_handle_t *semaphore)

Destroy the semaphore instance.

rsi_error_t
rsi_semaphore_check_and_destroy(rsi_semaphore_handle_t *semaphore)

Checks whether the semaphore is created and destroys the semaphore instance, if its created.

rsi_error_t
rsi_semaphore_wait(rsi_semaphore_handle_t *semaphore, uint32_t timeout_ms)

Wait for semaphore.

rsi_error_t
rsi_semaphore_post_from_isr(rsi_semaphore_handle_t *semaphore)

Release semaphore, which is acquired from ISR context.

rsi_error_t
rsi_semaphore_post(rsi_semaphore_handle_t *semaphore)

Release semaphore, which is acquired.

rsi_error_t
rsi_semaphore_reset(rsi_semaphore_handle_t *semaphore)

Function Documentation#

rsi_critical_section_entry#

rsi_reg_flags_t rsi_critical_section_entry (void )

Enter critical section.

Parameters
[in]

Returns

  • Interrupt status before entering critical section


Definition at line 31 of file driver/rsi_os_none.c

rsi_critical_section_exit#

rsi_critical_section_exit (rsi_reg_flags_t xflags)

Exit critical section by restoring interrupts.

Parameters
[in]xflags

- Interrupt status to restore interrupt on exit from critical section

Returns

  • Void


Definition at line 52 of file driver/rsi_os_none.c

rsi_virtual_to_physical_address#

void * rsi_virtual_to_physical_address (void * virtual_address)

Map virtual address to physical address.

Parameters
[in]virtual_address

- pointer to virtual address

Returns

  • void


Definition at line 72 of file driver/rsi_os_none.c

rsi_physical_to_virtual_address#

void * rsi_physical_to_virtual_address (void * physical_address)

Map physical address to virtual address.

Parameters
[in]physical_address

- pointer to physical_address

Returns

  • void


Definition at line 85 of file driver/rsi_os_none.c

rsi_mutex_create#

rsi_error_t rsi_mutex_create (rsi_mutex_handle_t * mutex)

Create and initialize the mutex.

Parameters
[in]mutex

- Mutex handle pointer

Returns

  • 0 - Success

  • Negative Value - Failure


Definition at line 96 of file driver/rsi_os_none.c

rsi_mutex_lock#

rsi_error_t rsi_mutex_lock (volatile rsi_mutex_handle_t * mutex)

Lock the mutex.

Parameters
[in]mutex

- Mutex handle pointer

Returns

  • 0 - Success

  • Negative Value - Failure


Definition at line 108 of file driver/rsi_os_none.c

rsi_mutex_unlock#

rsi_error_t rsi_mutex_unlock (volatile rsi_mutex_handle_t * mutex)

Unlock the mutex.

Parameters
[in]mutex

- Mutex handle pointer

Returns

  • 0 - Success

  • Negative Value - Failure


Definition at line 120 of file driver/rsi_os_none.c

rsi_mutex_destroy#

rsi_error_t rsi_mutex_destroy (rsi_mutex_handle_t * mutex)

Destroy the mutex.

Parameters
[in]mutex

- Mutex handle pointer

Returns

  • 0 - Success

  • Negative Value - Failure


Definition at line 131 of file driver/rsi_os_none.c

rsi_semaphore_create#

rsi_error_t rsi_semaphore_create (rsi_semaphore_handle_t * semaphore, uint32_t count)

Create and initialize the semaphore instance.

Parameters
[in]semaphore

- Semaphore handle pointer

[in]count

- Resource count

Returns

  • 0 - Success

  • Negative Value - Failure


Definition at line 142 of file driver/rsi_os_none.c

Referenced by rsi_bt_cb_init , and rsi_select

rsi_semaphore_destroy#

rsi_error_t rsi_semaphore_destroy (rsi_semaphore_handle_t * semaphore)

Destroy the semaphore instance.

Parameters
[in]semaphore

- Semaphore handle pointer

Returns

  • 0 - Success

  • Negative Value - Failure


Definition at line 154 of file driver/rsi_os_none.c

rsi_semaphore_check_and_destroy#

rsi_error_t rsi_semaphore_check_and_destroy (rsi_semaphore_handle_t * semaphore)

Checks whether the semaphore is created and destroys the semaphore instance, if its created.

Parameters
[in]semaphore

- Semaphore handle pointer

Check whether the semaphore is created and destroy, if created.

Returns

  • 0 - Success

  • Negative Value - Failure

Returns

  • 0 - Success

  • Negative Value - Failure


Definition at line 169 of file driver/rsi_os_none.c

rsi_semaphore_wait#

rsi_error_t rsi_semaphore_wait (rsi_semaphore_handle_t * semaphore, uint32_t timeout_ms)

Wait for semaphore.

Parameters
[in]semaphore

- Semaphore handle pointer

[in]timeout_ms

- Maximum time to wait to acquire semaphore. If timeout_ms is 0 then wait till semaphore is acquired.

Returns

  • 0 - Success

  • Negative Value - Failure


Definition at line 183 of file driver/rsi_os_none.c

rsi_semaphore_post_from_isr#

rsi_error_t rsi_semaphore_post_from_isr (rsi_semaphore_handle_t * semaphore)

Release semaphore, which is acquired from ISR context.

Parameters
[in]semaphore

- Semaphore handle pointer

Returns

  • 0 - Success

  • Negative Value - Failure


Definition at line 224 of file driver/rsi_os_none.c

rsi_semaphore_post#

rsi_error_t rsi_semaphore_post (rsi_semaphore_handle_t * semaphore)

Release semaphore, which is acquired.

Parameters
[in]semaphore

- Semaphore handle pointer

Returns

  • 0 - Success

  • Negative Value - Failure


Definition at line 238 of file driver/rsi_os_none.c

rsi_semaphore_reset#

rsi_error_t rsi_semaphore_reset (rsi_semaphore_handle_t * semaphore)
Parameters
N/Asemaphore

Definition at line 252 of file driver/rsi_os_none.c