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 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may 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,
elessair 0:f269e3021894 12 * WITHOUT 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 <stdio.h>
elessair 0:f269e3021894 17 #include "hal/gpio_api.h"
elessair 0:f269e3021894 18 #include "platform/wait_api.h"
elessair 0:f269e3021894 19 #include "platform/toolchain.h"
elessair 0:f269e3021894 20 #include "platform/mbed_interface.h"
elessair 0:f269e3021894 21 #include "platform/critical.h"
elessair 0:f269e3021894 22 #include "hal/serial_api.h"
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 #if DEVICE_SERIAL
elessair 0:f269e3021894 25 extern int stdio_uart_inited;
elessair 0:f269e3021894 26 extern serial_t stdio_uart;
elessair 0:f269e3021894 27 #endif
elessair 0:f269e3021894 28
elessair 0:f269e3021894 29 WEAK void mbed_die(void) {
elessair 0:f269e3021894 30 #if !defined (NRF51_H) && !defined(TARGET_EFM32)
elessair 0:f269e3021894 31 core_util_critical_section_enter();
elessair 0:f269e3021894 32 #endif
elessair 0:f269e3021894 33 #if (DEVICE_ERROR_RED == 1)
elessair 0:f269e3021894 34 gpio_t led_red; gpio_init_out(&led_red, LED_RED);
elessair 0:f269e3021894 35 #elif (DEVICE_ERROR_PATTERN == 1)
elessair 0:f269e3021894 36 gpio_t led_1; gpio_init_out(&led_1, LED1);
elessair 0:f269e3021894 37 gpio_t led_2; gpio_init_out(&led_2, LED2);
elessair 0:f269e3021894 38 gpio_t led_3; gpio_init_out(&led_3, LED3);
elessair 0:f269e3021894 39 gpio_t led_4; gpio_init_out(&led_4, LED4);
elessair 0:f269e3021894 40 #endif
elessair 0:f269e3021894 41
elessair 0:f269e3021894 42 while (1) {
elessair 0:f269e3021894 43 #if (DEVICE_ERROR_RED == 1)
elessair 0:f269e3021894 44 gpio_write(&led_red, 1);
elessair 0:f269e3021894 45
elessair 0:f269e3021894 46 #elif (DEVICE_ERROR_PATTERN == 1)
elessair 0:f269e3021894 47 gpio_write(&led_1, 1);
elessair 0:f269e3021894 48 gpio_write(&led_2, 0);
elessair 0:f269e3021894 49 gpio_write(&led_3, 0);
elessair 0:f269e3021894 50 gpio_write(&led_4, 1);
elessair 0:f269e3021894 51 #endif
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 wait_ms(150);
elessair 0:f269e3021894 54
elessair 0:f269e3021894 55 #if (DEVICE_ERROR_RED == 1)
elessair 0:f269e3021894 56 gpio_write(&led_red, 0);
elessair 0:f269e3021894 57
elessair 0:f269e3021894 58 #elif (DEVICE_ERROR_PATTERN == 1)
elessair 0:f269e3021894 59 gpio_write(&led_1, 0);
elessair 0:f269e3021894 60 gpio_write(&led_2, 1);
elessair 0:f269e3021894 61 gpio_write(&led_3, 1);
elessair 0:f269e3021894 62 gpio_write(&led_4, 0);
elessair 0:f269e3021894 63 #endif
elessair 0:f269e3021894 64
elessair 0:f269e3021894 65 wait_ms(150);
elessair 0:f269e3021894 66 }
elessair 0:f269e3021894 67 }
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 void mbed_error_printf(const char* format, ...) {
elessair 0:f269e3021894 70 va_list arg;
elessair 0:f269e3021894 71 va_start(arg, format);
elessair 0:f269e3021894 72 mbed_error_vfprintf(format, arg);
elessair 0:f269e3021894 73 va_end(arg);
elessair 0:f269e3021894 74 }
elessair 0:f269e3021894 75
elessair 0:f269e3021894 76 void mbed_error_vfprintf(const char * format, va_list arg) {
elessair 0:f269e3021894 77 #if DEVICE_SERIAL
elessair 0:f269e3021894 78 core_util_critical_section_enter();
elessair 0:f269e3021894 79 char buffer[128];
elessair 0:f269e3021894 80 int size = vsprintf(buffer, format, arg);
elessair 0:f269e3021894 81 if (size > 0) {
elessair 0:f269e3021894 82 if (!stdio_uart_inited) {
elessair 0:f269e3021894 83 serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
elessair 0:f269e3021894 84 }
elessair 0:f269e3021894 85 for (int i = 0; i < size; i++) {
elessair 0:f269e3021894 86 serial_putc(&stdio_uart, buffer[i]);
elessair 0:f269e3021894 87 }
elessair 0:f269e3021894 88 }
elessair 0:f269e3021894 89 core_util_critical_section_exit();
elessair 0:f269e3021894 90 #endif
elessair 0:f269e3021894 91 }