Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Critical Section HAL API specification #5346

Merged
merged 8 commits into from Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2015-2017, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cmsis.h"
#include "nrf_error.h"
#include "nrf_sdm.h"
#include "nrf_soc.h"

#include <stdbool.h>
#include <stdint.h>

static union {
uint32_t _PRIMASK_state;
uint8_t _sd_state;
} _state = {0};

static bool _use_softdevice_routine = false;
static bool _state_saved = false;

void hal_critical_section_enter(void)
{
// Fetch the current state of interrupts
uint32_t primask = __get_PRIMASK();
uint8_t temp_state = 0;

// If interrupts are enabled, try to use the soft device
uint8_t sd_enabled;
if ((primask == 0) &&
(sd_softdevice_is_enabled(&sd_enabled) == NRF_SUCCESS) &&
(sd_enabled == 1)) {
// If the softdevice can be used, use it.
sd_nvic_critical_region_enter(&temp_state);
_use_softdevice_routine = true;

if (_state_saved == false) {
_state._sd_state = temp_state;
}
} else {
// If interrupts are enabled, disable them.
if (primask == 0) {
__disable_irq();
}

// Store PRIMASK state, it will be restored when exiting critical
// section.
_use_softdevice_routine = false;

if (_state_saved == false) {
_state._PRIMASK_state = primask;
}
}

_state_saved = true;
}

void hal_critical_section_exit(void)
{
_state_saved = false;

// Restore the state as it was prior to entering the critical section.
if (_use_softdevice_routine) {
sd_nvic_critical_region_exit(_state._sd_state)
} else {
__set_PRIMASK(_state._PRIMASK_state);
}
}

bool hal_in_critical_section(void)
{
return (_state_saved == true);
}

This file was deleted.

107 changes: 107 additions & 0 deletions hal/critical_section_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/** \addtogroup hal */
/** @{*/
/* mbed Microcontroller Library
* Copyright (c) 2006-2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_CRITICAL_SECTION_API_H
#define MBED_CRITICAL_SECTION_API_H

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* \defgroup hal_critical Critical Section HAL functions
* @{
*/

/**
* Mark the start of a critical section
*
* This function will be called by core_util_critical_section_enter() each time
* the application requests to enter a critical section. The purpose of the
* critical section is to ensure mutual-exclusion synchronisation of the
* processor by preventing any change in processor control, the default
* behaviour requires storing the state of interrupts in the system before
* disabling them.
*
* The critical section code supports nesting. When a thread has entered a
* critical section it can make additional calls to
* core_util_critical_section_enter() without deadlocking itself. The critical
* section driver API tracks the number of nested calls to the critical section.
* The critical section will only be exited when
* core_util_critical_section_exit() has been called once for each time it
* entered the critical section.
*
* On the first call to enter a critical section this function MUST store the
* state of any interrupts or other application settings it will modify to
* facilitate the critical section.
*
* Each successive call to enter the critical section MUST ignore storing or
* modifying any application state.
*
* The default implementation of this function which will save the current state
* of interrupts before disabling them. This implementation can be found in
* mbed_critical_section_api.c. This behaviour is can be overridden on a per
* platform basis by providing a different implementation within the correct
* targets directory.
*/
void hal_critical_section_enter(void);


/** Mark the end of a critical section.
*
* The purpose of this function is to restore any state that was modified upon
* entering the critical section, allowing other threads or interrupts to change
* the processor control.
*
* This function will be called once by core_util_critical_section_exit() per
* critical section on last call to exit. When called, the application MUST
* restore the saved interrupt/application state that was saved when entering
* the critical section.
*
* There is a default implementation of this function, it will restore the state
* of interrupts that were previously saved when hal_critical_section_enter was
* first called, this implementation can be found in
* mbed_critical_section_api.c. This behaviour is overridable by providing a
* different function implementation within the correct targets directory.
*/
void hal_critical_section_exit(void);


/** Determine if the application is currently running in a critical section
*
* The purpose of this function is to inform the caller whether or not the
* application is running in a critical section. This is done by checking if
* the current interrupt state has been saved in the underlying implementation,
* this could also be done by checking the state of the interrupts at the time
* of calling.
*
* @return True if running in a critical section, false if not.
*/
bool hal_in_critical_section(void);


/**@}*/

#ifdef __cplusplus
}
#endif

#endif // MBED_CRITICAL_SECTION_API_H

/** @}*/
67 changes: 67 additions & 0 deletions hal/mbed_critical_section_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cmsis.h"
#include "hal/critical_section_api.h"
#include "platform/mbed_assert.h"
#include "platform/mbed_toolchain.h"

#include <stdbool.h>

static volatile bool critical_interrupts_enabled = false;
static volatile bool state_saved = false;

static bool are_interrupts_enabled(void)
{
#if defined(__CORTEX_A9)
return ((__get_CPSR() & 0x80) == 0);
#else
return ((__get_PRIMASK() & 0x1) == 0);
#endif
}


MBED_WEAK void hal_critical_section_enter(void)
{
const bool interrupt_state = are_interrupts_enabled();

__disable_irq();

if (state_saved == true) {
return;
}

critical_interrupts_enabled = interrupt_state;
state_saved = true;
}

MBED_WEAK void hal_critical_section_exit(void)
{
#ifndef FEATURE_UVISOR
// Interrupts must be disabled on invoking an exit from a critical section
MBED_ASSERT(!are_interrupts_enabled());
#endif
state_saved = false;

// Restore the IRQs to their state prior to entering the critical section
if (critical_interrupts_enabled == true) {
__enable_irq();
}
}

MBED_WEAK bool hal_in_critical_section(void)
{
return (state_saved == true);
}
Loading