BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Committer:
gustavatmel
Date:
Tue May 01 15:47:08 2018 +0000
Revision:
1:9c5af431a1f1
sdf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gustavatmel 1:9c5af431a1f1 1 /* mbed Microcontroller Library
gustavatmel 1:9c5af431a1f1 2 * Copyright (c) 2006-2012 ARM Limited
gustavatmel 1:9c5af431a1f1 3 *
gustavatmel 1:9c5af431a1f1 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
gustavatmel 1:9c5af431a1f1 5 * of this software and associated documentation files (the "Software"), to deal
gustavatmel 1:9c5af431a1f1 6 * in the Software without restriction, including without limitation the rights
gustavatmel 1:9c5af431a1f1 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
gustavatmel 1:9c5af431a1f1 8 * copies of the Software, and to permit persons to whom the Software is
gustavatmel 1:9c5af431a1f1 9 * furnished to do so, subject to the following conditions:
gustavatmel 1:9c5af431a1f1 10 *
gustavatmel 1:9c5af431a1f1 11 * The above copyright notice and this permission notice shall be included in
gustavatmel 1:9c5af431a1f1 12 * all copies or substantial portions of the Software.
gustavatmel 1:9c5af431a1f1 13 *
gustavatmel 1:9c5af431a1f1 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
gustavatmel 1:9c5af431a1f1 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
gustavatmel 1:9c5af431a1f1 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
gustavatmel 1:9c5af431a1f1 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
gustavatmel 1:9c5af431a1f1 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gustavatmel 1:9c5af431a1f1 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
gustavatmel 1:9c5af431a1f1 20 * SOFTWARE.
gustavatmel 1:9c5af431a1f1 21 */
gustavatmel 1:9c5af431a1f1 22 #include "rtos/Mutex.h"
gustavatmel 1:9c5af431a1f1 23 #include "rtos/Kernel.h"
gustavatmel 1:9c5af431a1f1 24
gustavatmel 1:9c5af431a1f1 25 #include <string.h>
gustavatmel 1:9c5af431a1f1 26 #include "mbed_error.h"
gustavatmel 1:9c5af431a1f1 27 #include "mbed_assert.h"
gustavatmel 1:9c5af431a1f1 28
gustavatmel 1:9c5af431a1f1 29 namespace rtos {
gustavatmel 1:9c5af431a1f1 30
gustavatmel 1:9c5af431a1f1 31 Mutex::Mutex(): _count(0)
gustavatmel 1:9c5af431a1f1 32 {
gustavatmel 1:9c5af431a1f1 33 constructor();
gustavatmel 1:9c5af431a1f1 34 }
gustavatmel 1:9c5af431a1f1 35
gustavatmel 1:9c5af431a1f1 36 Mutex::Mutex(const char *name)
gustavatmel 1:9c5af431a1f1 37 {
gustavatmel 1:9c5af431a1f1 38 constructor(name);
gustavatmel 1:9c5af431a1f1 39 }
gustavatmel 1:9c5af431a1f1 40
gustavatmel 1:9c5af431a1f1 41 void Mutex::constructor(const char *name)
gustavatmel 1:9c5af431a1f1 42 {
gustavatmel 1:9c5af431a1f1 43 memset(&_obj_mem, 0, sizeof(_obj_mem));
gustavatmel 1:9c5af431a1f1 44 osMutexAttr_t attr = { 0 };
gustavatmel 1:9c5af431a1f1 45 attr.name = name ? name : "aplication_unnamed_mutex";
gustavatmel 1:9c5af431a1f1 46 attr.cb_mem = &_obj_mem;
gustavatmel 1:9c5af431a1f1 47 attr.cb_size = sizeof(_obj_mem);
gustavatmel 1:9c5af431a1f1 48 attr.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust;
gustavatmel 1:9c5af431a1f1 49 _id = osMutexNew(&attr);
gustavatmel 1:9c5af431a1f1 50 MBED_ASSERT(_id);
gustavatmel 1:9c5af431a1f1 51 }
gustavatmel 1:9c5af431a1f1 52
gustavatmel 1:9c5af431a1f1 53 osStatus Mutex::lock(uint32_t millisec) {
gustavatmel 1:9c5af431a1f1 54 osStatus status = osMutexAcquire(_id, millisec);
gustavatmel 1:9c5af431a1f1 55 if (osOK == status) {
gustavatmel 1:9c5af431a1f1 56 _count++;
gustavatmel 1:9c5af431a1f1 57 }
gustavatmel 1:9c5af431a1f1 58 return status;
gustavatmel 1:9c5af431a1f1 59 }
gustavatmel 1:9c5af431a1f1 60
gustavatmel 1:9c5af431a1f1 61 bool Mutex::trylock() {
gustavatmel 1:9c5af431a1f1 62 return trylock_for(0);
gustavatmel 1:9c5af431a1f1 63 }
gustavatmel 1:9c5af431a1f1 64
gustavatmel 1:9c5af431a1f1 65 bool Mutex::trylock_for(uint32_t millisec) {
gustavatmel 1:9c5af431a1f1 66 osStatus status = lock(millisec);
gustavatmel 1:9c5af431a1f1 67 if (status == osOK) {
gustavatmel 1:9c5af431a1f1 68 return true;
gustavatmel 1:9c5af431a1f1 69 }
gustavatmel 1:9c5af431a1f1 70
gustavatmel 1:9c5af431a1f1 71 MBED_ASSERT(status == osErrorTimeout || status == osErrorResource);
gustavatmel 1:9c5af431a1f1 72
gustavatmel 1:9c5af431a1f1 73 return false;
gustavatmel 1:9c5af431a1f1 74 }
gustavatmel 1:9c5af431a1f1 75
gustavatmel 1:9c5af431a1f1 76 bool Mutex::trylock_until(uint64_t millisec) {
gustavatmel 1:9c5af431a1f1 77 uint64_t now = Kernel::get_ms_count();
gustavatmel 1:9c5af431a1f1 78
gustavatmel 1:9c5af431a1f1 79 if (now >= millisec) {
gustavatmel 1:9c5af431a1f1 80 return trylock();
gustavatmel 1:9c5af431a1f1 81 } else if (millisec - now >= osWaitForever) {
gustavatmel 1:9c5af431a1f1 82 // API permits early return
gustavatmel 1:9c5af431a1f1 83 return trylock_for(osWaitForever - 1);
gustavatmel 1:9c5af431a1f1 84 } else {
gustavatmel 1:9c5af431a1f1 85 return trylock_for(millisec - now);
gustavatmel 1:9c5af431a1f1 86 }
gustavatmel 1:9c5af431a1f1 87 }
gustavatmel 1:9c5af431a1f1 88
gustavatmel 1:9c5af431a1f1 89 osStatus Mutex::unlock() {
gustavatmel 1:9c5af431a1f1 90 _count--;
gustavatmel 1:9c5af431a1f1 91 return osMutexRelease(_id);
gustavatmel 1:9c5af431a1f1 92 }
gustavatmel 1:9c5af431a1f1 93
gustavatmel 1:9c5af431a1f1 94 osThreadId Mutex::get_owner() {
gustavatmel 1:9c5af431a1f1 95 return osMutexGetOwner(_id);
gustavatmel 1:9c5af431a1f1 96 }
gustavatmel 1:9c5af431a1f1 97
gustavatmel 1:9c5af431a1f1 98 Mutex::~Mutex() {
gustavatmel 1:9c5af431a1f1 99 osMutexDelete(_id);
gustavatmel 1:9c5af431a1f1 100 }
gustavatmel 1:9c5af431a1f1 101
gustavatmel 1:9c5af431a1f1 102 }