testharness for my semphore class

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
roberto_b
Date:
Tue Sep 14 20:38:26 2010 +0000
Commit message:

Changed in this revision

Semaphore.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r a2eb037fbb76 Semaphore.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Semaphore.h	Tue Sep 14 20:38:26 2010 +0000
@@ -0,0 +1,34 @@
+//  Copyright (C) 2010 Robert M. Bouwens
+
+#ifndef _SEMAPHORE_H_
+#define _SEMAPHORE_H_
+
+/*
+ * http://mbed.org/forum/mbed/topic/181/#comment-799
+ */
+
+class Semaphore {
+public:
+    Semaphore(): s(SemFree) {};
+
+    bool try_enter() {
+        int oldval = __ldrex(&s);
+        if (oldval == SemTaken) {
+            __clrex();
+            return false;
+        }
+        s = SemTaken;
+        __clrex();
+        return SemTaken == s;
+    };
+
+    void release() {
+        s = SemFree;
+    };
+
+private:
+    enum { SemFree = 1, SemTaken = 2 };
+    volatile int s;
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r a2eb037fbb76 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Sep 14 20:38:26 2010 +0000
@@ -0,0 +1,106 @@
+/**
+ * Test program for a bug. (http://mbed.org/forum/bugs-suggestions/topic/1074/)
+ *
+ * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
+ * http://shinta.main.jp/
+ */
+
+#include "mbed.h"
+#include "Semaphore.h"
+
+Serial ser(USBTX, USBRX);
+
+DigitalOut interrupt_data_led(LED1);
+DigitalOut dataled(LED2);
+DigitalOut data_locked_led(LED3);
+DigitalOut no_data_led(LED4);
+
+Semaphore sem;
+
+volatile char shared_resource = 'a';   // This is a shared resource for example.
+
+#define LOCK() sem.try_enter()
+#define UNLOCK() sem.release()
+
+void toggle_led( DigitalOut &led) {
+    if ( 1 == led)
+        led = 0;
+    else
+        led = 1;
+}
+
+char* my_itoa(int val, int base) {
+    static char buf[12] = {0};
+
+    int i = 10;
+    if ( 0 != val) {
+        for (; val && i ; --i, val /= base)
+            buf[i] = "0123456789abcdef"[val % base];
+        return &buf[i+1];
+    } else {
+        buf[0] = '0';
+        buf[1] = '0';
+        return &buf[0];
+    }
+}
+
+void waitBeforeSend(char ch) {
+    while ( 0 == ser.writeable()) {
+        wait_ms(1);
+    }
+    ser.putc( ch );
+    toggle_led(dataled);
+}
+
+
+/**
+ * A call back function for serial interrupt.
+ */
+void func_serial_interrupt(void) {
+    toggle_led(interrupt_data_led);
+    if (LOCK()) {
+        shared_resource = ser.getc();
+        UNLOCK();
+    } else {
+        toggle_led(no_data_led);
+    }
+}
+
+/**
+ * Entry point.
+ */
+int main() {
+    int i = 0;
+    char *out = "my semaphore test program\r\n";
+    ser.attach(&func_serial_interrupt);
+    char *ptr = out;
+    while ( *ptr != 0 ) {
+        waitBeforeSend(*ptr);
+        ptr++;
+    }
+
+    while (1) {
+        /*
+         * Hung up serial communication if you hit keys on console for Serial.
+         */
+        if (LOCK()) {
+            ptr = my_itoa(shared_resource, 16);
+            UNLOCK();
+            while ( *ptr != 0 ) {
+                waitBeforeSend(*ptr);
+                ptr++;
+            }
+            i++;
+            i %= 20;
+            if (!i) {
+                waitBeforeSend('\r');
+                waitBeforeSend('\n');
+            } else {
+                waitBeforeSend(' ');
+            }
+        } else {
+            toggle_led(data_locked_led);
+        }
+        wait_ms(100);
+    }
+}
diff -r 000000000000 -r a2eb037fbb76 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Sep 14 20:38:26 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e