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 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
elessair 0:f269e3021894 3 * SPDX-License-Identifier: Apache-2.0
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the License); you may
elessair 0:f269e3021894 5 * not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
elessair 0:f269e3021894 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16 #include "eventOS_event.h"
elessair 0:f269e3021894 17 #include "eventOS_event_timer.h"
elessair 0:f269e3021894 18 #include "nsdynmemLIB.h"
elessair 0:f269e3021894 19 #include "ns_list.h"
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 #define STARTUP_EVENT 0
elessair 0:f269e3021894 22 #define TIMER_EVENT 1
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 // Timeout structure, already typedefed to timeout_t
elessair 0:f269e3021894 25 struct timeout_entry_t {
elessair 0:f269e3021894 26 void (*callback)(void *);
elessair 0:f269e3021894 27 void *arg;
elessair 0:f269e3021894 28 uint8_t event_id;
elessair 0:f269e3021894 29 ns_list_link_t link;
elessair 0:f269e3021894 30 };
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32 static NS_LIST_HEAD(timeout_t, link) timeout_list = NS_LIST_INIT(timeout_list);
elessair 0:f269e3021894 33 static int8_t timeout_tasklet_id = -1;
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 static void timeout_tasklet(arm_event_s *event)
elessair 0:f269e3021894 36 {
elessair 0:f269e3021894 37 if (TIMER_EVENT != event->event_type) {
elessair 0:f269e3021894 38 return;
elessair 0:f269e3021894 39 }
elessair 0:f269e3021894 40
elessair 0:f269e3021894 41 timeout_t *found = NULL;
elessair 0:f269e3021894 42 ns_list_foreach_safe(timeout_t, cur, &timeout_list) {
elessair 0:f269e3021894 43 if (cur->event_id == event->event_id) {
elessair 0:f269e3021894 44 found = cur;
elessair 0:f269e3021894 45 ns_list_remove(&timeout_list, cur);
elessair 0:f269e3021894 46 break;
elessair 0:f269e3021894 47 }
elessair 0:f269e3021894 48 }
elessair 0:f269e3021894 49
elessair 0:f269e3021894 50 if (found) {
elessair 0:f269e3021894 51 found->callback(found->arg);
elessair 0:f269e3021894 52 ns_dyn_mem_free(found);
elessair 0:f269e3021894 53 }
elessair 0:f269e3021894 54 }
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 timeout_t *eventOS_timeout_ms(void (*callback)(void *), uint32_t ms, void *arg)
elessair 0:f269e3021894 57 {
elessair 0:f269e3021894 58 uint16_t count;
elessair 0:f269e3021894 59 uint8_t index;
elessair 0:f269e3021894 60 timeout_t *e = ns_dyn_mem_alloc(sizeof(timeout_t));
elessair 0:f269e3021894 61 if (!e) {
elessair 0:f269e3021894 62 return NULL;
elessair 0:f269e3021894 63 }
elessair 0:f269e3021894 64 e->callback = callback;
elessair 0:f269e3021894 65 e->arg = arg;
elessair 0:f269e3021894 66
elessair 0:f269e3021894 67 // Start timeout taskled if it is not running
elessair 0:f269e3021894 68 if (-1 == timeout_tasklet_id) {
elessair 0:f269e3021894 69 timeout_tasklet_id = eventOS_event_handler_create(timeout_tasklet, STARTUP_EVENT);
elessair 0:f269e3021894 70 if (timeout_tasklet_id < 0) {
elessair 0:f269e3021894 71 timeout_tasklet_id = -1;
elessair 0:f269e3021894 72 goto FAIL;
elessair 0:f269e3021894 73 }
elessair 0:f269e3021894 74 }
elessair 0:f269e3021894 75
elessair 0:f269e3021894 76 // Check that we still have indexes left. We have only 8bit timer id.
elessair 0:f269e3021894 77 count = ns_list_count(&timeout_list);
elessair 0:f269e3021894 78 if (count >= UINT8_MAX) { // Too big list, timer_id is uint8_t
elessair 0:f269e3021894 79 goto FAIL;
elessair 0:f269e3021894 80 }
elessair 0:f269e3021894 81
elessair 0:f269e3021894 82 // Find next free index
elessair 0:f269e3021894 83 index = 0;
elessair 0:f269e3021894 84 AGAIN:
elessair 0:f269e3021894 85 ns_list_foreach(timeout_t, cur, &timeout_list) {
elessair 0:f269e3021894 86 if (cur->event_id == index) { // This index was used
elessair 0:f269e3021894 87 index++; // Check next one.
elessair 0:f269e3021894 88 goto AGAIN; // Start checking from begining of the list, indexes are not in order
elessair 0:f269e3021894 89 }
elessair 0:f269e3021894 90 }
elessair 0:f269e3021894 91 e->event_id = index;
elessair 0:f269e3021894 92 ns_list_add_to_end(&timeout_list, e);
elessair 0:f269e3021894 93 eventOS_event_timer_request(index, TIMER_EVENT, timeout_tasklet_id, ms);
elessair 0:f269e3021894 94 return e;
elessair 0:f269e3021894 95 FAIL:
elessair 0:f269e3021894 96 ns_dyn_mem_free(e);
elessair 0:f269e3021894 97 return NULL;
elessair 0:f269e3021894 98 }
elessair 0:f269e3021894 99
elessair 0:f269e3021894 100 void eventOS_timeout_cancel(timeout_t *t)
elessair 0:f269e3021894 101 {
elessair 0:f269e3021894 102 ns_list_foreach_safe(timeout_t, cur, &timeout_list) {
elessair 0:f269e3021894 103 if (t == cur) {
elessair 0:f269e3021894 104 ns_list_remove(&timeout_list, cur);
elessair 0:f269e3021894 105 eventOS_event_timer_cancel(cur->event_id, timeout_tasklet_id);
elessair 0:f269e3021894 106 ns_dyn_mem_free(cur);
elessair 0:f269e3021894 107 }
elessair 0:f269e3021894 108 }
elessair 0:f269e3021894 109 }