mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*
elessair 0:f269e3021894 2 * Implementation for Posix compliant platforms
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Copyright (c) 2016 Christopher Haster
elessair 0:f269e3021894 5 *
elessair 0:f269e3021894 6 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 7 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 8 * You may obtain a copy of the License at
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 11 *
elessair 0:f269e3021894 12 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 13 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 15 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 16 * limitations under the License.
elessair 0:f269e3021894 17 */
elessair 0:f269e3021894 18 #include "equeue/equeue_platform.h"
elessair 0:f269e3021894 19
elessair 0:f269e3021894 20 #if defined(EQUEUE_PLATFORM_POSIX)
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #include <time.h>
elessair 0:f269e3021894 23 #include <sys/time.h>
elessair 0:f269e3021894 24 #include <errno.h>
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26
elessair 0:f269e3021894 27 // Tick operations
elessair 0:f269e3021894 28 unsigned equeue_tick(void) {
elessair 0:f269e3021894 29 struct timeval tv;
elessair 0:f269e3021894 30 gettimeofday(&tv, 0);
elessair 0:f269e3021894 31 return (unsigned)(tv.tv_sec*1000 + tv.tv_usec/1000);
elessair 0:f269e3021894 32 }
elessair 0:f269e3021894 33
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 // Mutex operations
elessair 0:f269e3021894 36 int equeue_mutex_create(equeue_mutex_t *m) {
elessair 0:f269e3021894 37 return pthread_mutex_init(m, 0);
elessair 0:f269e3021894 38 }
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 void equeue_mutex_destroy(equeue_mutex_t *m) {
elessair 0:f269e3021894 41 pthread_mutex_destroy(m);
elessair 0:f269e3021894 42 }
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 void equeue_mutex_lock(equeue_mutex_t *m) {
elessair 0:f269e3021894 45 pthread_mutex_lock(m);
elessair 0:f269e3021894 46 }
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 void equeue_mutex_unlock(equeue_mutex_t *m) {
elessair 0:f269e3021894 49 pthread_mutex_unlock(m);
elessair 0:f269e3021894 50 }
elessair 0:f269e3021894 51
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 // Semaphore operations
elessair 0:f269e3021894 54 int equeue_sema_create(equeue_sema_t *s) {
elessair 0:f269e3021894 55 int err = pthread_mutex_init(&s->mutex, 0);
elessair 0:f269e3021894 56 if (err) {
elessair 0:f269e3021894 57 return err;
elessair 0:f269e3021894 58 }
elessair 0:f269e3021894 59
elessair 0:f269e3021894 60 err = pthread_cond_init(&s->cond, 0);
elessair 0:f269e3021894 61 if (err) {
elessair 0:f269e3021894 62 return err;
elessair 0:f269e3021894 63 }
elessair 0:f269e3021894 64
elessair 0:f269e3021894 65 s->signal = false;
elessair 0:f269e3021894 66 return 0;
elessair 0:f269e3021894 67 }
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 void equeue_sema_destroy(equeue_sema_t *s) {
elessair 0:f269e3021894 70 pthread_cond_destroy(&s->cond);
elessair 0:f269e3021894 71 pthread_mutex_destroy(&s->mutex);
elessair 0:f269e3021894 72 }
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 void equeue_sema_signal(equeue_sema_t *s) {
elessair 0:f269e3021894 75 pthread_mutex_lock(&s->mutex);
elessair 0:f269e3021894 76 s->signal = true;
elessair 0:f269e3021894 77 pthread_cond_signal(&s->cond);
elessair 0:f269e3021894 78 pthread_mutex_unlock(&s->mutex);
elessair 0:f269e3021894 79 }
elessair 0:f269e3021894 80
elessair 0:f269e3021894 81 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
elessair 0:f269e3021894 82 pthread_mutex_lock(&s->mutex);
elessair 0:f269e3021894 83 if (!s->signal) {
elessair 0:f269e3021894 84 if (ms < 0) {
elessair 0:f269e3021894 85 pthread_cond_wait(&s->cond, &s->mutex);
elessair 0:f269e3021894 86 } else {
elessair 0:f269e3021894 87 struct timeval tv;
elessair 0:f269e3021894 88 gettimeofday(&tv, 0);
elessair 0:f269e3021894 89
elessair 0:f269e3021894 90 struct timespec ts = {
elessair 0:f269e3021894 91 .tv_sec = ms/1000 + tv.tv_sec,
elessair 0:f269e3021894 92 .tv_nsec = ms*1000000 + tv.tv_usec*1000,
elessair 0:f269e3021894 93 };
elessair 0:f269e3021894 94
elessair 0:f269e3021894 95 pthread_cond_timedwait(&s->cond, &s->mutex, &ts);
elessair 0:f269e3021894 96 }
elessair 0:f269e3021894 97 }
elessair 0:f269e3021894 98
elessair 0:f269e3021894 99 bool signal = s->signal;
elessair 0:f269e3021894 100 s->signal = false;
elessair 0:f269e3021894 101 pthread_mutex_unlock(&s->mutex);
elessair 0:f269e3021894 102
elessair 0:f269e3021894 103 return signal;
elessair 0:f269e3021894 104 }
elessair 0:f269e3021894 105
elessair 0:f269e3021894 106 #endif