MCU driver/HAL for the Picocell Gateway concentrator board. The firmware implements either a USB CDC protocol or a UART protocol to bridge commands coming from host to the SX1308 SPI interface.

Committer:
dgabino
Date:
Wed Apr 11 14:42:47 2018 +0000
Revision:
0:c76361bd82e8
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgabino 0:c76361bd82e8 1 /* mbed Microcontroller Library
dgabino 0:c76361bd82e8 2 * Copyright (c) 2006-2016 ARM Limited
dgabino 0:c76361bd82e8 3 *
dgabino 0:c76361bd82e8 4 * Licensed under the Apache License, Version 2.0 (the "License");
dgabino 0:c76361bd82e8 5 * you may not use this file except in compliance with the License.
dgabino 0:c76361bd82e8 6 * You may obtain a copy of the License at
dgabino 0:c76361bd82e8 7 *
dgabino 0:c76361bd82e8 8 * http://www.apache.org/licenses/LICENSE-2.0
dgabino 0:c76361bd82e8 9 *
dgabino 0:c76361bd82e8 10 * Unless required by applicable law or agreed to in writing, software
dgabino 0:c76361bd82e8 11 * distributed under the License is distributed on an "AS IS" BASIS,
dgabino 0:c76361bd82e8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dgabino 0:c76361bd82e8 13 * See the License for the specific language governing permissions and
dgabino 0:c76361bd82e8 14 * limitations under the License.
dgabino 0:c76361bd82e8 15 */
dgabino 0:c76361bd82e8 16
dgabino 0:c76361bd82e8 17 #include <stdlib.h>
dgabino 0:c76361bd82e8 18 #include <stdarg.h>
dgabino 0:c76361bd82e8 19 #include <stdio.h>
dgabino 0:c76361bd82e8 20 #include "mbed_mem_trace.h"
dgabino 0:c76361bd82e8 21 #include "critical.h"
dgabino 0:c76361bd82e8 22
dgabino 0:c76361bd82e8 23 /******************************************************************************
dgabino 0:c76361bd82e8 24 * Internal variables, functions and helpers
dgabino 0:c76361bd82e8 25 *****************************************************************************/
dgabino 0:c76361bd82e8 26
dgabino 0:c76361bd82e8 27 /* The callback function that will be called after a traced memory operations finishes. */
dgabino 0:c76361bd82e8 28 static mbed_mem_trace_cb_t mem_trace_cb;
dgabino 0:c76361bd82e8 29 /* 'trave_level' guards "trace inside trace" situations (for example, the implementation
dgabino 0:c76361bd82e8 30 * of realloc() might call malloc() internally, and since malloc() is also traced, this could
dgabino 0:c76361bd82e8 31 * result in two calls to the callback function instead of one. */
dgabino 0:c76361bd82e8 32 static uint8_t trace_level;
dgabino 0:c76361bd82e8 33
dgabino 0:c76361bd82e8 34 /******************************************************************************
dgabino 0:c76361bd82e8 35 * Public interface
dgabino 0:c76361bd82e8 36 *****************************************************************************/
dgabino 0:c76361bd82e8 37
dgabino 0:c76361bd82e8 38 void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb) {
dgabino 0:c76361bd82e8 39 mem_trace_cb = cb;
dgabino 0:c76361bd82e8 40 }
dgabino 0:c76361bd82e8 41
dgabino 0:c76361bd82e8 42 void *mbed_mem_trace_malloc(void *res, size_t size, void *caller) {
dgabino 0:c76361bd82e8 43 if (mem_trace_cb) {
dgabino 0:c76361bd82e8 44 if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
dgabino 0:c76361bd82e8 45 mem_trace_cb(MBED_MEM_TRACE_MALLOC, res, caller, size);
dgabino 0:c76361bd82e8 46 }
dgabino 0:c76361bd82e8 47 core_util_atomic_decr_u8(&trace_level, 1);
dgabino 0:c76361bd82e8 48 }
dgabino 0:c76361bd82e8 49 return res;
dgabino 0:c76361bd82e8 50 }
dgabino 0:c76361bd82e8 51
dgabino 0:c76361bd82e8 52 void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller) {
dgabino 0:c76361bd82e8 53 if (mem_trace_cb) {
dgabino 0:c76361bd82e8 54 if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
dgabino 0:c76361bd82e8 55 mem_trace_cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size);
dgabino 0:c76361bd82e8 56 }
dgabino 0:c76361bd82e8 57 core_util_atomic_decr_u8(&trace_level, 1);
dgabino 0:c76361bd82e8 58 }
dgabino 0:c76361bd82e8 59 return res;
dgabino 0:c76361bd82e8 60 }
dgabino 0:c76361bd82e8 61
dgabino 0:c76361bd82e8 62 void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller) {
dgabino 0:c76361bd82e8 63 if (mem_trace_cb) {
dgabino 0:c76361bd82e8 64 if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
dgabino 0:c76361bd82e8 65 mem_trace_cb(MBED_MEM_TRACE_CALLOC, res, caller, num, size);
dgabino 0:c76361bd82e8 66 }
dgabino 0:c76361bd82e8 67 core_util_atomic_decr_u8(&trace_level, 1);
dgabino 0:c76361bd82e8 68 }
dgabino 0:c76361bd82e8 69 return res;
dgabino 0:c76361bd82e8 70 }
dgabino 0:c76361bd82e8 71
dgabino 0:c76361bd82e8 72 void mbed_mem_trace_free(void *ptr, void *caller) {
dgabino 0:c76361bd82e8 73 if (mem_trace_cb) {
dgabino 0:c76361bd82e8 74 if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
dgabino 0:c76361bd82e8 75 mem_trace_cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr);
dgabino 0:c76361bd82e8 76 }
dgabino 0:c76361bd82e8 77 core_util_atomic_decr_u8(&trace_level, 1);
dgabino 0:c76361bd82e8 78 }
dgabino 0:c76361bd82e8 79 }
dgabino 0:c76361bd82e8 80
dgabino 0:c76361bd82e8 81 void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...) {
dgabino 0:c76361bd82e8 82 va_list va;
dgabino 0:c76361bd82e8 83 size_t temp_s1, temp_s2;
dgabino 0:c76361bd82e8 84 void *temp_ptr;
dgabino 0:c76361bd82e8 85
dgabino 0:c76361bd82e8 86 va_start(va, caller);
dgabino 0:c76361bd82e8 87 switch(op) {
dgabino 0:c76361bd82e8 88 case MBED_MEM_TRACE_MALLOC:
dgabino 0:c76361bd82e8 89 temp_s1 = va_arg(va, size_t);
dgabino 0:c76361bd82e8 90 printf(MBED_MEM_DEFAULT_TRACER_PREFIX "m:%p;%p-%u\n", res, caller, temp_s1);
dgabino 0:c76361bd82e8 91 break;
dgabino 0:c76361bd82e8 92
dgabino 0:c76361bd82e8 93 case MBED_MEM_TRACE_REALLOC:
dgabino 0:c76361bd82e8 94 temp_ptr = va_arg(va, void*);
dgabino 0:c76361bd82e8 95 temp_s1 = va_arg(va, size_t);
dgabino 0:c76361bd82e8 96 printf(MBED_MEM_DEFAULT_TRACER_PREFIX "r:%p;%p-%p;%u\n", res, caller, temp_ptr, temp_s1);
dgabino 0:c76361bd82e8 97 break;
dgabino 0:c76361bd82e8 98
dgabino 0:c76361bd82e8 99 case MBED_MEM_TRACE_CALLOC:
dgabino 0:c76361bd82e8 100 temp_s1 = va_arg(va, size_t);
dgabino 0:c76361bd82e8 101 temp_s2 = va_arg(va, size_t);
dgabino 0:c76361bd82e8 102 printf(MBED_MEM_DEFAULT_TRACER_PREFIX "c:%p;%p-%u;%u\n", res, caller, temp_s1, temp_s2);
dgabino 0:c76361bd82e8 103 break;
dgabino 0:c76361bd82e8 104
dgabino 0:c76361bd82e8 105 case MBED_MEM_TRACE_FREE:
dgabino 0:c76361bd82e8 106 temp_ptr = va_arg(va, void*);
dgabino 0:c76361bd82e8 107 printf(MBED_MEM_DEFAULT_TRACER_PREFIX "f:%p;%p-%p\n", res, caller, temp_ptr);
dgabino 0:c76361bd82e8 108 break;
dgabino 0:c76361bd82e8 109
dgabino 0:c76361bd82e8 110 default:
dgabino 0:c76361bd82e8 111 printf("?\n");
dgabino 0:c76361bd82e8 112 }
dgabino 0:c76361bd82e8 113 va_end(va);
dgabino 0:c76361bd82e8 114 }
dgabino 0:c76361bd82e8 115