Quantcast
Channel: ARM assembly inline C mutex impelmentation - Stack Overflow
Viewing all articles
Browse latest Browse all 3

ARM assembly inline C mutex impelmentation

$
0
0

I am working on an embedded systems project using the Xilinx Zedboard. The board has the ability to asymmetrically split it's dual core ARM A9 processor to run two separate programs simultaneously. I've configured the board to run Linux on one core and a bare metal application on the other acting as a hardware controller. For inter processor communication I am utilizing on chip memory that is shared between the two processors. I'm struggling with my lock implementation and I'm curious if anyone experience with such things or may be able to point me in the right direction.

I found a mutex implementation on the ARM reference website http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dht0008a/ch01s03s02.html and I have adapted it to C inline assembly. After testing the lock function seems to hang and I have no idea why. My experience with assembly is somewhat limited, I've seen it in school and understand the high level concepts but I'm completely lost in the low level implementation.

Does this look right or is this even the right approach? All I need is a simple mechanism that will allow me to perform atomic operations on my inter process communication structures (several message queues).

mutex.h

#ifndef __OCM_MUTEX_H__#define __OCM_MUTEX_H__#include <stdint.h>#define LOCKED      1#define UNLOCKED    0typedef uint32_t mutex_t;extern void ocm_lock_mutex(volatile mutex_t* mutex);extern void ocm_unlock_mutex(volatile mutex_t* mutex);#endif

mutex.c

#include "mutex.h"void ocm_lock_mutex(volatile mutex_t* mutex) {    int result;    const uint32_t locked = LOCKED;    __asm__ __volatile__("@ocm_lock_mutex\n""1: LDREX   %[r2], [%[r0]]\n""   CMP     %[r2], %[locked]\n""   BEQ     2f\n""   STREXNE %[r2], %[locked], [%[r0]]\n""   CMP     %[r2], #1\n""   BEQ     1b\n""   DMB\n""   B       3f\n""2: WFE\n""   B       1b\n""3: NOP\n"    : [r2] "=r" (result), [r0] "=r" (mutex)    : [locked] "r" (locked));}void ocm_unlock_mutex(volatile mutex_t* mutex) {    const uint32_t unlocked = UNLOCKED;    __asm__ __volatile__("@ocm_unlock_mutex\n""   DMB\n""   STR %[unlocked], [%[r0]]\n""   DSB\n""   SEV\n"    : [r0] "=r" (mutex)    : [unlocked] "r" (unlocked));}

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>