RTEMS: bsps/arm/atsam/contrib/utils/utility.h Source File

| RTEMS 5.4-rc2

Loading...
Searching...
No Matches
utility.h
1/* ----------------------------------------------------------------------------
2 * SAM Software Package License
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2014, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30#ifndef UTILITY_H
31#define UTILITY_H
32
33#include "chip.h"
34
35
36
37#define RESET_CYCLE_COUNTER() do { \
38 CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk; \
39 __DSB(); DWT->LAR = 0xC5ACCE55; __DSB(); \
40 DWT->CTRL &= ~DWT_CTRL_CYCCNTENA_Msk; \
41 DWT->CYCCNT = 0; \
42 DWT->CTRL = DWT_CTRL_CYCCNTENA_Msk; \
43 }while(0)
44
45#define GET_CYCLE_COUNTER(x) x=DWT->CYCCNT;
46
47#define LockMutex(mut, timeout) get_lock(&mut, 1, &timeout)
48
49#define ReleaseMutex(mut) free_lock(&mut)
50
51#define GetResource(mut, max, timeout) get_lock(&mut, max, &timeout)
52
53#define FreeResource(mut) free_lock(&mut)
54
55
56__STATIC_INLINE uint8_t Is_LockFree(volatile uint8_t *Lock_Variable)
57{
58 /* return Variable value*/
59 return __LDREXB(Lock_Variable);
60
61}
62
63__STATIC_INLINE uint8_t get_lock(volatile uint8_t *Lock_Variable, const uint8_t maxValue, volatile uint32_t *pTimeout)
64{
65 while (*pTimeout)
66 {
67 if(__LDREXB(Lock_Variable) < maxValue)
68 {
69 /* Set the Variable */
70 while( __STREXB(((*Lock_Variable) + 1), Lock_Variable) )
71 {
72 if(!(*pTimeout)--)
73 {
74 return 1; // quit if timeout
75 }
76 }
77 /* Memory access barrier */
78 __DMB();
79 TRACE_DEBUG("Mutex locked ");
80 return 0;
81 }
82
83 ((*pTimeout)--);
84 }
85 return 1;
86}
87
88
89
90__STATIC_INLINE uint8_t free_lock(volatile uint8_t *Lock_Variable)
91{
92 /* Memory access barrier Ensure memory operations completed before releasing lock */
93 __DSB();
94 if(__LDREXB(Lock_Variable))
95 {
96 __STREXB( ((*Lock_Variable) - 1), Lock_Variable);
97 TRACE_DEBUG("Mutex freed ");
98 __DSB();
99 __DMB(); // Ensure memory operations completed before
100 return 0;
101 }
102 else
103 {
104 return 1;
105 }
106
107
108}
109
110
111#endif /* UTILITY_H */
TRACE_DEBUG
#define TRACE_DEBUG(...)
Definition: trace.h:183