Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Tue Mar 09 13:10:40 2021 +0000
Revision:
3:6fe17b8a6d62
Parent:
0:4beb2ea291ec
SDBlockDevice added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
boro 0:4beb2ea291ec 1 /* mbed Microcontroller Library
boro 0:4beb2ea291ec 2 * Copyright (c) 2006-2012 ARM Limited
boro 0:4beb2ea291ec 3 *
boro 0:4beb2ea291ec 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
boro 0:4beb2ea291ec 5 * of this software and associated documentation files (the "Software"), to deal
boro 0:4beb2ea291ec 6 * in the Software without restriction, including without limitation the rights
boro 0:4beb2ea291ec 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
boro 0:4beb2ea291ec 8 * copies of the Software, and to permit persons to whom the Software is
boro 0:4beb2ea291ec 9 * furnished to do so, subject to the following conditions:
boro 0:4beb2ea291ec 10 *
boro 0:4beb2ea291ec 11 * The above copyright notice and this permission notice shall be included in
boro 0:4beb2ea291ec 12 * all copies or substantial portions of the Software.
boro 0:4beb2ea291ec 13 *
boro 0:4beb2ea291ec 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
boro 0:4beb2ea291ec 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
boro 0:4beb2ea291ec 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
boro 0:4beb2ea291ec 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
boro 0:4beb2ea291ec 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
boro 0:4beb2ea291ec 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
boro 0:4beb2ea291ec 20 * SOFTWARE.
boro 0:4beb2ea291ec 21 */
boro 0:4beb2ea291ec 22 #include "rtos/Semaphore.h"
boro 0:4beb2ea291ec 23 #include "platform/mbed_assert.h"
boro 0:4beb2ea291ec 24
boro 0:4beb2ea291ec 25 #include <string.h>
boro 0:4beb2ea291ec 26
boro 0:4beb2ea291ec 27 namespace rtos {
boro 0:4beb2ea291ec 28
boro 0:4beb2ea291ec 29 Semaphore::Semaphore(int32_t count) {
boro 0:4beb2ea291ec 30 constructor(count, 0xffff);
boro 0:4beb2ea291ec 31 }
boro 0:4beb2ea291ec 32
boro 0:4beb2ea291ec 33 Semaphore::Semaphore(int32_t count, uint16_t max_count) {
boro 0:4beb2ea291ec 34 constructor(count, max_count);
boro 0:4beb2ea291ec 35 }
boro 0:4beb2ea291ec 36
boro 0:4beb2ea291ec 37 void Semaphore::constructor(int32_t count, uint16_t max_count) {
boro 0:4beb2ea291ec 38 memset(&_obj_mem, 0, sizeof(_obj_mem));
boro 0:4beb2ea291ec 39 osSemaphoreAttr_t attr = { 0 };
boro 0:4beb2ea291ec 40 attr.cb_mem = &_obj_mem;
boro 0:4beb2ea291ec 41 attr.cb_size = sizeof(_obj_mem);
boro 0:4beb2ea291ec 42 _id = osSemaphoreNew(max_count, count, &attr);
boro 0:4beb2ea291ec 43 MBED_ASSERT(_id != NULL);
boro 0:4beb2ea291ec 44 }
boro 0:4beb2ea291ec 45
boro 0:4beb2ea291ec 46 int32_t Semaphore::wait(uint32_t millisec) {
boro 0:4beb2ea291ec 47 osStatus_t stat = osSemaphoreAcquire(_id, millisec);
boro 0:4beb2ea291ec 48 switch (stat) {
boro 0:4beb2ea291ec 49 case osOK:
boro 0:4beb2ea291ec 50 return osSemaphoreGetCount(_id) + 1;
boro 0:4beb2ea291ec 51 case osErrorTimeout:
boro 0:4beb2ea291ec 52 case osErrorResource:
boro 0:4beb2ea291ec 53 return 0;
boro 0:4beb2ea291ec 54 case osErrorParameter:
boro 0:4beb2ea291ec 55 default:
boro 0:4beb2ea291ec 56 return -1;
boro 0:4beb2ea291ec 57 }
boro 0:4beb2ea291ec 58 }
boro 0:4beb2ea291ec 59
boro 0:4beb2ea291ec 60 osStatus Semaphore::release(void) {
boro 0:4beb2ea291ec 61 return osSemaphoreRelease(_id);
boro 0:4beb2ea291ec 62 }
boro 0:4beb2ea291ec 63
boro 0:4beb2ea291ec 64 Semaphore::~Semaphore() {
boro 0:4beb2ea291ec 65 osSemaphoreDelete(_id);
boro 0:4beb2ea291ec 66 }
boro 0:4beb2ea291ec 67
boro 0:4beb2ea291ec 68 }