Shinichiro Nakamura / Mbed 2 deprecated TestProgramForBugTopic1074

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Test program for a bug. (http://mbed.org/forum/bugs-suggestions/topic/1074/)
00003  *
00004  * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
00005  * http://shinta.main.jp/
00006  */
00007 
00008 #include "mbed.h"
00009 #include "Semaphore.h"
00010 
00011 Serial ser(USBTX, USBRX);
00012 Ticker ticker;
00013 BusOut led(LED4, LED3, LED2, LED1);
00014 Semaphore sem;
00015 
00016 volatile char shared_resource;   // This is a shared resource for example.
00017 
00018 #define LOCK() sem.try_enter()
00019 #define UNLOCK() sem.release()
00020 
00021 /**
00022  * a ticker.
00023  */
00024 void func_tick(void) {
00025     led = led + 1;
00026 }
00027 
00028 /**
00029  * A call back function for serial interrupt.
00030  */
00031 void func_serial_interrupt(void) {
00032     if (LOCK()) {
00033         shared_resource = ser.getc();
00034         UNLOCK();
00035     }
00036 }
00037 
00038 /**
00039  * Entry point.
00040  */
00041 int main() {
00042     ticker.attach_us(&func_tick, 100 * 1000);
00043     ser.attach(&func_serial_interrupt);
00044     while (1) {
00045         /*
00046          * Hung up serial communication if you hit keys on console for Serial.
00047          */
00048         if (LOCK()) {
00049             if (ser.writeable()) {
00050                 ser.printf("0x%x\n", shared_resource);
00051             }
00052             UNLOCK();
00053         }
00054         wait_ms(100);
00055     }
00056 }