Fork of LCD-Window which works with Enhanced TextLCD from Wim
Fork of LcdWindow by
semaphore.cpp@10:d40c70908bf0, 2016-01-04 (annotated)
- Committer:
- charly
- Date:
- Mon Jan 04 20:22:17 2016 +0000
- Revision:
- 10:d40c70908bf0
- Parent:
- 9:2fe93daa2106
Adapted for EnhancedLCD. Disabled semaphore as there are problem with new mbed-library.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hlipka | 3:e5d5e2fe4bf6 | 1 | /** |
| hlipka | 3:e5d5e2fe4bf6 | 2 | * code from Igor Skochinsky |
| hlipka | 3:e5d5e2fe4bf6 | 3 | * taken from http://mbed.org/forum/mbed/post/799/ |
| hlipka | 3:e5d5e2fe4bf6 | 4 | */ |
| hlipka | 3:e5d5e2fe4bf6 | 5 | |
| hlipka | 3:e5d5e2fe4bf6 | 6 | #include "semaphore.h" |
| hlipka | 3:e5d5e2fe4bf6 | 7 | |
| hlipka | 9:2fe93daa2106 | 8 | Semaphore::Semaphore(): s(SemFree) {}; |
| hlipka | 9:2fe93daa2106 | 9 | bool Semaphore::_abort=false; |
| hlipka | 9:2fe93daa2106 | 10 | |
| hlipka | 9:2fe93daa2106 | 11 | bool Semaphore::take(bool block) { |
| hlipka | 9:2fe93daa2106 | 12 | if (_abort) |
| hlipka | 9:2fe93daa2106 | 13 | block=false; |
| hlipka | 3:e5d5e2fe4bf6 | 14 | int oldval; |
| charly | 10:d40c70908bf0 | 15 | /* |
| hlipka | 3:e5d5e2fe4bf6 | 16 | #if defined(TARGET_LPC1768) // on Cortex-M3 we can use ldrex/strex |
| hlipka | 3:e5d5e2fe4bf6 | 17 | do { |
| hlipka | 9:2fe93daa2106 | 18 | // read the semaphore value |
| hlipka | 9:2fe93daa2106 | 19 | oldval = __ldrex(&s); |
| hlipka | 9:2fe93daa2106 | 20 | // loop again if it is locked and we are blocking |
| hlipka | 9:2fe93daa2106 | 21 | // or setting it with strex failed |
| hlipka | 9:2fe93daa2106 | 22 | } while ( (block && oldval == SemTaken) || __strex(SemTaken, &s) != 0 ); |
| hlipka | 3:e5d5e2fe4bf6 | 23 | if ( !block ) __clrex(); // clear exclusive lock set by ldrex |
| hlipka | 3:e5d5e2fe4bf6 | 24 | #else // on arm7 there's only swp |
| charly | 10:d40c70908bf0 | 25 | |
| hlipka | 3:e5d5e2fe4bf6 | 26 | do { |
| hlipka | 9:2fe93daa2106 | 27 | // swp sets the pointed data to the given value and returns the previous one |
| hlipka | 9:2fe93daa2106 | 28 | oldval = __swp(SemTaken, &s); |
| hlipka | 9:2fe93daa2106 | 29 | // if blocking, loop until the previous value becomes 0 |
| hlipka | 9:2fe93daa2106 | 30 | // which would mean we have successfully taken the lock |
| hlipka | 9:2fe93daa2106 | 31 | } while (block && oldval == SemTaken); |
| charly | 10:d40c70908bf0 | 32 | //#endif |
| charly | 10:d40c70908bf0 | 33 | |
| hlipka | 3:e5d5e2fe4bf6 | 34 | return oldval == SemFree; |
| charly | 10:d40c70908bf0 | 35 | */ |
| charly | 10:d40c70908bf0 | 36 | return true; |
| hlipka | 9:2fe93daa2106 | 37 | } |
| hlipka | 9:2fe93daa2106 | 38 | |
| hlipka | 9:2fe93daa2106 | 39 | // release the semaphore |
| hlipka | 9:2fe93daa2106 | 40 | void Semaphore::release() { |
| hlipka | 3:e5d5e2fe4bf6 | 41 | s = SemFree; |
| hlipka | 9:2fe93daa2106 | 42 | } |
