/*******************************************************************************
* @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.
*
******************************************************************************/

#ifndef RSI_WITH_OS
/*
  Include files
 */
#include <rsi_driver.h>

/*==============================================*/
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

Disable interrupt to enter crtical 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)

Take the mutex.

rsi_error_t
rsi_mutex_unlock(volatile rsi_mutex_handle_t *mutex)

Give the mutex.

rsi_error_t
rsi_mutex_destroy(rsi_mutex_handle_t *mutex)

Destroye 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)

Check whether semaphore is created or not, It destroys the semaphore instance,if its created, otherwise should return Success.

rsi_error_t
rsi_semaphore_wait(rsi_semaphore_handle_t *semaphore, uint32_t timeout_ms)

Wireless library to acquire or wait for semaphore.

rsi_error_t
rsi_semaphore_post_from_isr(rsi_semaphore_handle_t *semaphore)

Wireless library to release semaphore, which was acquired.

rsi_error_t
rsi_semaphore_post(rsi_semaphore_handle_t *semaphore)

Wireless library to release semaphore, which was acquired.

rsi_error_t
rsi_semaphore_reset(rsi_semaphore_handle_t *semaphore)

Used by Wireless Library to reset the semaphore.

Function Documentation#

rsi_critical_section_entry#

rsi_reg_flags_t rsi_critical_section_entry (void)

Disable interrupt to enter crtical section.

Parameters
[in]

Returns

  • flags - interrupt status before entering critical section


Definition at line 32 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 53 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 73 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 86 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 97 of file driver/rsi_os_none.c

rsi_mutex_lock#

rsi_error_t rsi_mutex_lock (volatile rsi_mutex_handle_t *mutex)

Take the mutex.

Parameters
[in]mutex

- Mutex handle pointer

Returns

  • 0 - Success Negative Value - Failure


Definition at line 109 of file driver/rsi_os_none.c

rsi_mutex_unlock#

rsi_error_t rsi_mutex_unlock (volatile rsi_mutex_handle_t *mutex)

Give the mutex.

Parameters
[in]mutex

- Mutex handle pointer

Returns

  • 0 - Success Negative Value - Failure


Definition at line 121 of file driver/rsi_os_none.c

rsi_mutex_destroy#

rsi_error_t rsi_mutex_destroy (rsi_mutex_handle_t *mutex)

Destroye the mutex.

Parameters
[in]mutex

- Mutex handle pointer

Returns

  • 0 - Success Negative Value - Failure


Definition at line 132 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 143 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 155 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)

Check whether semaphore is created or not, It destroys the semaphore instance,if its created, otherwise should return Success.

Parameters
[in]semaphore

- Semaphore handle pointer

Returns

  • 0 - Success Negative Value - Failure


Definition at line 170 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)

Wireless library to acquire or 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 acquire semaphore.

Returns

  • 0 - Success Negative Value - Failure


Definition at line 184 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)

Wireless library to release semaphore, which was acquired.

Parameters
[in]semaphore

- Semaphore handle pointer

Returns

  • 0 - Success Negative Value - Failure


Definition at line 225 of file driver/rsi_os_none.c

rsi_semaphore_post#

rsi_error_t rsi_semaphore_post (rsi_semaphore_handle_t *semaphore)

Wireless library to release semaphore, which was acquired.

Parameters
[in]semaphore

- Semaphore handle pointer

Returns

  • 0 - Success Negative Value - Failure


Definition at line 239 of file driver/rsi_os_none.c

rsi_semaphore_reset#

rsi_error_t rsi_semaphore_reset (rsi_semaphore_handle_t *semaphore)

Used by Wireless Library to reset the semaphore.

Parameters
[in]semaphore

- Semaphore handle pointer

Returns

  • 0 - Success Negative Value - Failure


Definition at line 253 of file driver/rsi_os_none.c