Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * |
| 4 | * \brief Critical sections related functionality declaration. |
| 5 | * |
| 6 | * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries. |
| 7 | * |
| 8 | * \asf_license_start |
| 9 | * |
| 10 | * \page License |
| 11 | * |
| 12 | * Subject to your compliance with these terms, you may use Microchip |
| 13 | * software and any derivatives exclusively with Microchip products. |
| 14 | * It is your responsibility to comply with third party license terms applicable |
| 15 | * to your use of third party software (including open source software) that |
| 16 | * may accompany Microchip software. |
| 17 | * |
| 18 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, |
| 19 | * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, |
| 20 | * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, |
| 21 | * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE |
| 22 | * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL |
| 23 | * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE |
| 24 | * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE |
| 25 | * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT |
| 26 | * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY |
| 27 | * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, |
| 28 | * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. |
| 29 | * |
| 30 | * \asf_license_stop |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #ifndef _HAL_ATOMIC_H_INCLUDED |
| 35 | #define _HAL_ATOMIC_H_INCLUDED |
| 36 | |
| 37 | #include <compiler.h> |
| 38 | |
| 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
| 43 | /** |
| 44 | * \addtogroup doc_driver_hal_helper_atomic |
| 45 | * |
| 46 | *@{ |
| 47 | */ |
| 48 | |
| 49 | /** |
| 50 | * \brief Type for the register holding global interrupt enable flag |
| 51 | */ |
| 52 | typedef uint32_t hal_atomic_t; |
| 53 | |
| 54 | /** |
| 55 | * \brief Helper macro for entering critical sections |
| 56 | * |
| 57 | * This macro is recommended to be used instead of a direct call |
| 58 | * hal_enterCritical() function to enter critical |
| 59 | * sections. No semicolon is required after the macro. |
| 60 | * |
| 61 | * \section atomic_usage Usage Example |
| 62 | * \code |
| 63 | * CRITICAL_SECTION_ENTER() |
| 64 | * Critical code |
| 65 | * CRITICAL_SECTION_LEAVE() |
| 66 | * \endcode |
| 67 | */ |
| 68 | #define CRITICAL_SECTION_ENTER() \ |
| 69 | { \ |
| 70 | volatile hal_atomic_t __atomic; \ |
| 71 | atomic_enter_critical(&__atomic); |
| 72 | |
| 73 | /** |
| 74 | * \brief Helper macro for leaving critical sections |
| 75 | * |
| 76 | * This macro is recommended to be used instead of a direct call |
| 77 | * hal_leaveCritical() function to leave critical |
| 78 | * sections. No semicolon is required after the macro. |
| 79 | */ |
| 80 | #define CRITICAL_SECTION_LEAVE() \ |
| 81 | atomic_leave_critical(&__atomic); \ |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * \brief Disable interrupts, enter critical section |
| 86 | * |
| 87 | * Disables global interrupts. Supports nested critical sections, |
| 88 | * so that global interrupts are only re-enabled |
| 89 | * upon leaving the outermost nested critical section. |
| 90 | * |
| 91 | * \param[out] atomic The pointer to a variable to store the value of global |
| 92 | * interrupt enable flag |
| 93 | */ |
| 94 | void atomic_enter_critical(hal_atomic_t volatile *atomic); |
| 95 | |
| 96 | /** |
| 97 | * \brief Exit atomic section |
| 98 | * |
| 99 | * Enables global interrupts. Supports nested critical sections, |
| 100 | * so that global interrupts are only re-enabled |
| 101 | * upon leaving the outermost nested critical section. |
| 102 | * |
| 103 | * \param[in] atomic The pointer to a variable, which stores the latest stored |
| 104 | * value of the global interrupt enable flag |
| 105 | */ |
| 106 | void atomic_leave_critical(hal_atomic_t volatile *atomic); |
| 107 | |
| 108 | /** |
| 109 | * \brief Retrieve the current driver version |
| 110 | * |
| 111 | * \return Current driver version. |
| 112 | */ |
| 113 | uint32_t atomic_get_version(void); |
| 114 | /**@}*/ |
| 115 | |
| 116 | #ifdef __cplusplus |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | #endif /* _HAL_ATOMIC_H_INCLUDED */ |