libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
sync.c
Go to the documentation of this file.
1/*
2 * This file is part of the libopencm3 project.
3 *
4 * Copyright (C) 2012 Fergus Noble <fergusnoble@gmail.com>
5 *
6 * This library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <libopencm3/cm3/sync.h>
21
22/* DMB is supported on CM0 */
23void __dmb()
24{
25 __asm__ volatile ("dmb");
26}
27
28/* Those are defined only on CM3 or CM4 */
29#if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
30
31uint32_t __ldrex(volatile uint32_t *addr)
32{
33 uint32_t res;
34 __asm__ volatile ("ldrex %0, [%1]" : "=r" (res) : "r" (addr));
35 return res;
36}
37
38uint32_t __strex(uint32_t val, volatile uint32_t *addr)
39{
40 uint32_t res;
41 __asm__ volatile ("strex %0, %2, [%1]"
42 : "=&r" (res) : "r" (addr), "r" (val));
43 return res;
44}
45
46void mutex_lock(mutex_t *m)
47{
48 while (!mutex_trylock(m));
49}
50
51/* returns 1 if the lock was acquired */
52uint32_t mutex_trylock(mutex_t *m)
53{
54 uint32_t status = 1;
55
56 /* If the mutex is unlocked. */
57 if (__ldrex(m) == MUTEX_UNLOCKED) {
58 /* Try to lock it. */
59 status = __strex(MUTEX_LOCKED, m);
60 }
61
62 /* Execute the mysterious Data Memory Barrier instruction! */
63 __dmb();
64
65 /* Did we get the lock? If not then try again
66 * by calling this function once more. */
67 return status == 0;
68}
69
70void mutex_unlock(mutex_t *m)
71{
72 /* Ensure accesses to protected resource are finished */
73 __dmb();
74
75 /* Free the lock. */
76 *m = MUTEX_UNLOCKED;
77}
78
79#endif
void __dmb()
Definition: sync.c:23