5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
group-onsemi
Date:
Wed Jan 25 20:34:15 2017 +0000
Revision:
0:098463de4c5d
Initial commit

Who changed what in which revision?

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